I'm trying to write a generic interpolate
method that works on any type that has two methods, a *
and a +
, like this:
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
}
This doesn't work though (on Scala 2.8.0.RC7), I get the following error messages:
<console>:8: error: recursive method + needs result type
def +(v: V): V
^
<console>:7: error: recursive method * needs result type
def *(t: Double): V
^
How do I specify the structural type correctly? (Or is there a better way to do this?)
Surely you could solve this problem using the typeclasses approach (of e.g. Scalaz):
Then obviously you would need a (implicit) typeclass conversion in scope for all the types you cared about:
Then this can be run easily:
AFAIK, this is not possible. This was one of my own first questions.