1

在将我的项目从 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
  }
}
4

1 回答 1

1

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

当您更改 in 上的界限时TNodeCode您会丢失先前界限是与N绑定到的细化的信息this.type

最后一局获胜。

这是我一直在思考的问题。

这可能不适合您的需求:

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}}

编辑:这似乎没有帮助,对不起。其他事情正在发生。

于 2014-01-07T18:56:55.383 回答