【发布时间】:2014-07-28 19:17:21
【问题描述】:
如果我这样做:
object Parent {
class Inner extends Testable { type Self <: Inner }
def inner = new Inner()
}
object Child {
class Inner extends Parent.Inner { type Self <: Inner }
def inner = new Inner()
}
trait Testable {
type Self
def test[T <: Self] = {}
}
object Main {
// this works
val p: Parent.Inner = Child.inner
// this doesn't
val h = Parent.inner
h.test[Child.Inner]
}
我收到此错误:
error: type arguments [Child.Inner] do not conform to method test's type parameter bounds [T <: Main.h.Self]
h.test[Child.Inner]
当我的 Self 类型为 Parent.Inner 和 Child.Inner <: parent.inner>
如果我将type Self <: Inner 更改为type Self = Inner 然后override type Self = Inner,我会收到此错误:
overriding type Self in class Inner, which equals Parent.Inner;
type Self has incompatible type
class Inner extends Parent.Inner { override type Self = Inner }
【问题讨论】:
标签: scala inheritance polymorphism inner-classes abstract-type