【发布时间】:2014-03-14 19:21:48
【问题描述】:
考虑:
trait SuperBar { def superBarMethod = ??? }
trait Bar extends SuperBar
trait FooWithSelfType { this: Bar =>
super.superBarMethod // error: value superBarMethod is not a member of AnyRef
}
trait FooWithExtends extends Bar {
super.superBarMethod
}
这种限制是由于一些底层实现的缺陷,还是出于某种原因实际上是这样设计的?
我的看法是,如果已知this 是Bar 类型,并且已知Bar 是SuperBar 的子类型,那么在this 上调用任何SuperBar 方法应该允许。
【问题讨论】:
-
为什么需要使用
super.限定对superBarMethod的调用。它已经可用,无需使用super.,因为您已声明自己需要Bar可用。
标签: scala inheritance traits self-type