我正在尝试编写一个泛型interpolate方法,该方法适用于任何具有两种方法的类型,a *和a +,如下所示:
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
}
Run Code Online (Sandbox Code Playgroud)
这不起作用(在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
^
Run Code Online (Sandbox Code Playgroud)
如何正确指定结构类型?(或者有更好的方法吗?)