trait Mixin {}
case class A(a: Int) extends Mixin
case object B extends Mixin
trait Base {
val m = A(1)
}
trait Sub extends Base {
override val m = B // does. not. compute.
}
我的特質Sub
得到一個編譯錯誤:
<console>:18: error: overriding value m in trait Base of type A;
value m has incompatible type
override val m = B // does. not. compute.
^
爲什麼?案例對象繼承了正確的特質,就像案例類一樣。這是Scala 2.10。
我可能會建議您聲明'Base.m'作爲'def'而不是'val'。重疊性狀的val可能會導致各種初始化順序的困境。 –