以下代码:
trait Foo {
type T
val x: T
}
trait Bar {
type F <: Foo { type T <: F }
}
class C[B <: Bar](val f: B#F) {
val x: f.T = f.x
}
Run Code Online (Sandbox Code Playgroud)
被Scala编译器(2.11.5)拒绝,并显示以下错误消息:
error: type mismatch;
found : C.this.f.x.type (with underlying type C.this.f.T)
required: this.T
val x: f.T = f.x
^
Run Code Online (Sandbox Code Playgroud)
如果省略显式类型声明,则根据typer阶段的输出正确推断类型:
private[this] val x: C.this.f.T = C.this.f.x;
<stable> <accessor> def x: C.this.f.T = C.this.x
Run Code Online (Sandbox Code Playgroud)
如果F
在绑定内部Bar
更改为不是其成员的类型Bar
,即,该问题也会消失
type F <: …
Run Code Online (Sandbox Code Playgroud)