def someA
(在 trait B
中)如何使用具有相同 C#MyType
的 trait A
在 B 中? (然后A#MyType =:= B#MyType
)
trait C {
type MyType
}
trait A {
self: C =>
def doSomething(s: MyType) { println(s.toString)}
}
trait B {
self: C =>
def someA: A
def myType: MyType
def action = someA.doSomething(myType)
}
// Mix part
case class Ahoy(value: String)
trait ConcreteC extends C {
type MyType = Ahoy
}
class PieceOfCake extends B with ConcreteC {
val someA = new A with ConcreteC
val myType = Ahoy("MyType")
}
它无法编译:类型不匹配;
[error] found : B.this.MyType
[error] required: _1.MyType where val _1: A
[error] def action = someA.doSomething(myType))
最佳答案
您可以声明 doSomething
和 myType
以使用 MyType
的路径独立版本,SomeType#MyType
:
trait SomeType {
type MyType
}
trait A {
self: SomeType =>
def doSomething(s: SomeType#MyType) { println(s.toString)}
}
trait B {
self: SomeType =>
def someA: A
def myType: SomeType#MyType
def action = someA.doSomething(myType)
}
关于scala - 蛋糕图案和类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10831329/