我有参数化类型的课程
abstract class Worker[T] {
def conf1: ...
def conf2: ...
def doWork ...
}
abstract class SpecializedWorker[T: TypeTag] extends Worker[T] {
//some behavior overriden (used fields from Trait that i want create)
}
我想创建可以混合到工人的特质。
trait Extension {
self: Worker[_] =>
def someParameter: ... // only several workers can have that. thats why i need trait
def produceSpecializedWorker = new SpecializedWorker[???]() {}
}
如何从self中提取类型信息以替换???
答案 0 :(得分:1)
这是一种提取类型参数的方法:
trait Extension {
self: Worker[_] =>
def mkSpWorker[T](implicit ev: this.type <:< Worker[T]) = new SpecializedWorker[T]() {}
}
但我不推荐:)你可以在Worker
中定义一个可以在Extension
中使用的类型成员。
abstract class Worker[T] {
type TT = T
}
trait Extension {
self: Worker[_] =>
def mkSpWorker = new SpecializedWorker[TT]() {}
}
或者你可以考虑给Extension
一个类型参数。我认为这不是什么大问题。