【发布时间】:2013-06-18 10:05:40
【问题描述】:
我正在定义一个可操作的类型:
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 参数应该相同。为什么不是这样?
【问题讨论】:
-
我认为 Régis 给你的答案(使用 F 有界多态性)是可行的方法,但值得注意的是编译器会对
<:<而不是=:=感到满意。跨度>