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

Mat*_*ias 0 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.
}
Run Code Online (Sandbox Code Playgroud)

我在特征中得到了编译错误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.
                      ^
Run Code Online (Sandbox Code Playgroud)

为什么?case对象继承了正确的trait,就像case类一样.这是Scala 2.10.

Lee*_*Lee 6

你需要制作m类型Mixin:

 trait Base {
    val m: Mixin = A(1)
 }
Run Code Online (Sandbox Code Playgroud)