2014-02-28 3 views
2

에 따라 클래스 : 나는 다음과 같은 클래스가 있다고 가정 : 기본적으로스칼라는 것은 스칼라 가능합니다 궁금 매개 변수

val buyOr: BuyCondition = new OrCondition[BuyCondition](bc1, bc2) 
val sellOr: SellCondition = new OrCondition[SellCondition](sc1, sc2) 

:

trait Condition 
trait BuyCondition extends Condition 
trait SellCondition extends Condition 

class OrCondition[C <: Condition](c1: C, c2: C) extends Condition 

하는 것은 다음과 같은 작업을하려면이 가능하다 내가 OrCondition 중 하나를 판매하거나 그것을 하나의 매개 변수에 따라 구매 싶습니다.

답변

1

예는 단지 컴파일러

존재하는 예를 여기 phantom types

유형 BuyType 및 SellType 런타임에 통해 인스턴스되지 않습니다 때문에 팬텀 유형이라고

// Define a base trait for the condition phantom type 
object Condition { 
    trait Type 
} 

// The Condition is paramaterised by the phantom type 
trait Condition[T <: Condition.Type] 

// Define Buy/Sell types 
trait BuyType extends Condition.Type 
trait SellType extends Condition.Type 

// defin the buy/Sell conditions 
type BuyCondition = Condition[BuyType] 
type SellCondition = Condition[SellType] 

class OrCondition[T <: Condition.Type](c1: Condition[T], c2: Condition[T]) extends Condition[T] 

val bc1, bc2 = new BuyCondition {} 
val sc1, sc2 = new SellCondition {} 

val buyOr: BuyCondition = new OrCondition(bc1, bc2) 
val sellOr: SellCondition = new OrCondition(sc1, sc2) 

참고한다 사용하여이 작업을 수행 할 수 있습니다

관련 문제