假设我有一个特征Foo [A,B].
我想定义一个类Bar,使用Foo进行参数化并在Bar方法中重用Foo类型参数.如下所示的内容(此代码段无法编译):
trait Bar[Foo[A, B]] {
def doSmth[C](A => C): C
}
有没有办法实现类似的东西,并且在Bar声明中只有一个类型参数?
附:如果有人可以为所描述的内容提出正确的术语,那也会很棒.
最佳答案 不确定,但也许这是你想要去的地方.
trait Foo[A,B]
trait Bar[A] { self: Foo[A,_] =>
def doSmth[C](atoc: A => C): C
}
或者,使用类型别名.
trait Foo[A,B] {type FooA = A}
trait Bar { self: Foo[_,_] =>
def doSmth[C](atoc: FooA => C): C
}
这意味着当混合中有一个Bar时需要Foo.