this.type错误编译?

时间:2014-01-07 17:07:16

标签: scala scala-2.11

将我的project从Scala 2.10移植到2.11时,我遇到了类型参数化的突然编译错误。我试图修改和精确定位;还有一些奇怪的错误。有人能解释一下吗 顺便说一下,我很满意这种回归(或进展),因为它激励我简化代码。

trait TNode {type N <:  Node {type T = this.type}}
trait  Node {type T <: TNode {type N = this.type}; def t: T}

trait TNodeCode[R] extends TNode {type N <:  NodeCode[R]; val code: ()=>N=>R}
trait  NodeCode[R] extends  Node {type T <: TNodeCode[R]}

object Test {
  def executeCode[R](n: Node, code: =>()=>R): R = {null.asInstanceOf[R]}

  def executeTCode[N <: NodeCode[R], R](n: N): R = {
    executeCode(n, ()=>n.t.code.apply.apply(n))
    // compile error:                       ^
    //   type mismatch; found: n.type (with underlying type N) required: _1.N where val _1: n.T
  }
}

1 个答案:

答案 0 :(得分:1)

我有一个仓促的部分答案:

当您更改TNodeCode的界限时,您将丢失上一个绑定是N绑定到this.type的精简的信息。

最后一次胜利。

This is the question我在想。

这可能不符合您的需求:

trait TNode0 {type N <:  Node0 }
trait  Node0 {type T <: TNode0 ; def t: T}

trait TNode extends TNode0 {type N <:  Node {type T = this.type}}
trait  Node extends Node0  {type T <: TNode {type N = this.type}}

trait TNodeCode[R] extends TNode0 {type N <:  NodeCode[R] {type T = this.type}; val code: ()=>N=>R}
trait  NodeCode[R] extends  Node0 {type T <: TNodeCode[R] {type N = this.type}}

编辑:这似乎没有帮助,抱歉。还有其他事情正在发生。