相关疑难解决方法(0)

Scala - 如何定义引用自身的结构类型?

我正在尝试编写一个泛型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)

如何正确指定结构类型?(或者有更好的方法吗?)

scala structural-typing

8
推荐指数
1
解决办法
871
查看次数

标签 统计

scala ×1

structural-typing ×1