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接口一样).但是没有编译器错误.
我的具体情况比较复杂.我无法测试运行时发生的事情.
你肯定会得到编译器错误......
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)