简体   繁体   English

编译器似乎在类型优化中忽略了类型绑定

[英]Compiler seems to ignore type bound in type refinement

The type refinement in the code below seems to say that the path-dependent type vt.ValueT includes this.type : 下面代码中的类型细化似乎表明与路径相关的类型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 */ }

but the Scala compiler (version 2.11.2) says: 但是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)
                                                             ^

Is there some reason why deducing that this.type <: vt.ValueT is illegitimate here? 有什么原因可以推断出this.type <: vt.ValueT在这里是非法的? Is there another way to tell the compiler what it needs to know? 还有另一种方法可以告诉编译器它需要知道什么吗?

I've tried putting the type refinement on the declaration of vt . 我试过将类型细化放在vt的声明中。 The compiler objects that the resulting type is volatile . 编译器反对结果类型为volatile Perhaps that's a clue to the problem. 也许这就是问题的线索。

The refinement { type ValueT = this.type } generates the same error message. 改进{ type ValueT = this.type }生成相同的错误消息。

I think the problem is that in the bound >: this.type , the binding for this gets muddled somehow by the compiler. 我认为问题是,在绑定>: this.type ,为结合this被莫名其妙地由编译器糊涂。 If I make the following changes (& remove override from asConstrainedValue ), compilation succeeds for me: 如果我进行以下更改(并从asConstrainedValue删除override ),则编译对我而言成功:

trait Value { self =>
  type ValueTypeT <: ValueType { type ValueT >: self.type }
  …

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM