Tim*_*ske 8 overriding scala abstract-type
给出以下代码:
class A {
class B
type C <: B
trait D
}
class E extends A {
type C = B
}
class F extends E {
override type C = B with D
}
Run Code Online (Sandbox Code Playgroud)
为什么Eclipse Indigo IDE中的Scala IDE的表示编译器会在E类中覆盖覆盖类型C的错误消息,它等于F.this.B; C型有不兼容的类型?
在所有类"B"仅用特征"D""修改"之后,因此两种类型定义具有相同的基本类型,即"B".因此兼容的类型定义.
以下代码有效.我认为类型赋值的规则与变量赋值类似,例如:
class Foo
trait Bar
val a: Foo = new Foo
val fooWithBar: Foo = new Foo with Bar
Run Code Online (Sandbox Code Playgroud)
我的理解错了吗?
Did*_*ont 13
它们不兼容,C型可能用于逆变位置
class E extends A {
type C = B
def f(c: C)
}
class F extends E {
override type C = B with D
def f(c: ???)
}
Run Code Online (Sandbox Code Playgroud)
补
给e: E
,你可以调用e.f(new B)
.如果e
是的话val e = new F
?