小编Mar*_*Łoś的帖子

路径依赖类型 - 以下代码有什么问题?

以下代码:

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)

types scala path-dependent-type

6
推荐指数
1
解决办法
99
查看次数

标签 统计

path-dependent-type ×1

scala ×1

types ×1