的我想写这样的代码:斯卡拉类扩展与通用性状这是一个类型的字段
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'。 –