编译器似乎忽略了类型细化中的类型绑定

Ben*_*itz 2 scala type-inference path-dependent-type type-bounds

下面代码中的类型细化似乎表明路径依赖类型vt.ValueT包括this.type:

trait ValueType {
  type ValueT <: Value

  type ConstrainedT <: ConstrainedValue

  def makeConstrainedValue(v: ValueT): ConstrainedT = ???
}

trait Value {
  type ValueTypeT <: ValueType { type ValueT >: this.type } // <--- HEY, COMPILER, READ THIS

  val vt: ValueTypeT

  def asConstrainedValue = vt.makeConstrainedValue(this) // <--- Compiler complains here
}

trait ConstrainedValue { /* details omitted */ }
Run Code Online (Sandbox Code Playgroud)

但Scala编译器(版本2.11.2)说:

error: type mismatch;
found   : Value.this.type (with underlying type Test.Value)
required: Value.this.vt.ValueT
   override def asConstrainedValue = vt.makeConstrainedValue(this)
                                                             ^
Run Code Online (Sandbox Code Playgroud)

有什么理由可以推断出这this.type <: vt.ValueT是非法的吗?还有另一种方法可以告诉编译器它需要知道什么吗?

我已经尝试将类型细化放在声明上vt.编译器对象生成的类型是volatile.也许这是问题的线索.

细化{ type ValueT = this.type }生成相同的错误消息.

Hug*_*ugh 5

我认为问题在于,在绑定中>: this.type,this编译器会以某种方式混淆绑定.如果我进行了以下更改(并override从中删除asConstrainedValue),编译成功对我来说:

trait Value { self =>
  type ValueTypeT <: ValueType { type ValueT >: self.type }
  …
Run Code Online (Sandbox Code Playgroud)