假设:
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.
^
为什么呢? case对象继承了正确的trait,就像case类一样。这是Scala 2.10。
答案 0 :(得分:6)
您需要m
类型Mixin
:
trait Base {
val m: Mixin = A(1)
}