为什么我不能用匹配类型的case对象覆盖val?

时间:2014-01-08 21:55:12

标签: scala

假设:

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。

1 个答案:

答案 0 :(得分:6)

您需要m类型Mixin

 trait Base {
    val m: Mixin = A(1)
 }