假设我有一个特征 Foo[A, B]
.
我想定义一个类 Bar
,参数化为 Foo
并重复使用 Foo
Bar
中的类型参数方法。如下所示(此代码段无法编译):
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
}
这意味着
Foo
当您有 Bar
时需要在混合。
关于scala - 是否可以将类型参数的类型参数传播到 Scala 中的参数化类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38725584/