的我想寫這樣的代碼:斯卡拉類擴展與通用性狀這是一個類型的字段
trait A[S]
class B {
class BI
}
class C(val b: B) extends A[b.BI] // won't compile
這將無法編譯。 所以我寫這個:
class C[BI0] private (val b: B) extends A[BI0]
object C {
def apply(b: B): C[b.BI] = new C(b)
}
但這看起來很醜。有更好的實施嗎?
爲什麼我有這個問題?我想到一個例子:
trait Store[Goods] {
def sell(goods: Goods): Unit
}
class CarFactory {
def make(): Car = new Car
class Car
}
class CarStore(val factory: CarFactory) extends Store[factory.Car]{//can't compile
def sell(car: factory.Car): Unit = {}
}
我不想使用CarFactory#Car
,因爲這款車店只賣工廠factory
的汽車。
什麼是BI0?一個具體的類型? –
@YuvalItzchakov我認爲這只是'C2'的類型參數的名稱... –
我想'BI0'是類型'b.BI'。 –