在mixin中,scala编译器可以强制实现抽象特征方法吗?

use*_*215 2 compiler-construction scala traits mixins abstract-methods

我有一个抽象方法和具体实现方法的特征,所以这样的事情:

trait MyTrait extends BaseClass {
    def myAbstractMethod: MyReturnType
    def myConcreteMethod = { /*implementation*/ }
}
Run Code Online (Sandbox Code Playgroud)

现在我混合了这个特质:

class MyClass extends BaseClass with MyTrait {

}
Run Code Online (Sandbox Code Playgroud)

BaseClass不实现抽象方法.我期望scala编译器在混合特征时强制执行抽象方法(就像Java接口一样).但是没有编译器错误.

我的具体情况比较复杂.我无法测试运行时发生的事情.

  1. 为什么scala编译器不强制执行抽象方法?
  2. 我可以让scala编译器强制执行抽象方法吗?
  3. 我必须在某处添加摘要或覆盖吗?
  4. 当我尝试创建和使用MyClass的实例时,在运行时会发生什么?

mer*_*no1 6

你肯定会得到编译器错误......

scala> :paste
// Entering paste mode (ctrl-D to finish)

trait MyTrait extends BaseClass {
    def myAbstractMethod: MyReturnType
    def myConcreteMethod = { /*implementation*/ }
}

class MyClass extends BaseClass with MyTrait {    
}


// Exiting paste mode, now interpreting.

<console>:14: error: class MyClass needs to be abstract, since method myAbstractMethod in trait MyTrait of type => MyReturnType is not defined
       class MyClass extends BaseClass with MyTrait {


 ^
Run Code Online (Sandbox Code Playgroud)