我正在尝试编写一个通用的interpolate
方法,它适用于任何具有两个方法*
和+
的类型,如下所示:
trait Container {
type V = {
def *(t: Double): V
def +(v: V): V
}
def interpolate(t: Double, a: V, b: V): V = a * (1.0 - t) + b * t
}
这个方法在 Scala 2.8.0.RC7 中不可用,会出现以下错误信息:
<console>:8: error: recursive method + needs result type
def +(v: V): V
^
<console>:7: error: recursive method * needs result type
def *(t: Double): V
^
我该如何正确指定结构类型?(或者有更好的方法吗?)