2013-06-18 20 views
3

斯卡拉不能证明我定义的操作类型类型相等

trait Operable { 
    def +[A](other: A)(implicit evidence: this.type =:= A): this.type 
    def -[A](other: A)(implicit evidence: this.type =:= A): this.type 
    def *[A](other: Float): this.type 
} 
/** Position descriptions */ 
trait Pos[T <: Operable] { 
    def eval: T 
} 
def test[T <: Operable](x1: Pos[T], x2: Pos[T]): T = { 
    x2.eval - x1.eval 
} 

而且我得到以下编译时错误:

Cannot prove that _1.type =:= T. 

为什么不能编译器证明式的平等,以及如何克服这个问题?来自x1和x2的T参数应该是相同的。为什么不是这样?

+1

我想雷吉斯给你(使用F-界多态性)的答案是要走的路,但值得一提的是,编译器会很乐意与'< :<而不是'=:='。 – Hugh

回答

5

this.type是特定实例的类型。它与任何其他实例都不兼容,即使实例类型完全相同。 所以基本上在这样的定义:

def -[A](other: A)(implicit evidence: this.type =:= A): this.type 

你的证据试图证明other是完全相同的实例作为this,这可能不是你在心中。

我想你会想重新设计你的设计。你可以尝试使用,而不是F-界多态性

trait Operable[Self <: Operable[Self]] { 
    def +(other: Self): Self 
    def -(other: Self): Self 
    def *(other: Float): Self 
} 
/** Position descriptions */ 
trait Pos[T <: Operable[T]] { 
    def eval: T 
} 
def test[T <: Operable[T]](x1: Pos[T], x2: Pos[T]): T = { 
    x2.eval - x1.eval 
} 
+0

你有描述F-Bounded多态性的好链接吗? –

+0

我可以建议Twitter的Scala的学校:http://twitter.github.io/scala_school/advanced-types.html#fbounded –