Type parameterization in Scala
- by horatius83
So I'm learning Scala at the moment, and I'm trying to create an abstract vector class with a vector-space of 3 (x,y,z coordinates). I'm trying to add two of these vectors together with the following code:
package math
class Vector3[T](ax:T,ay:T,az:T) {
def x = ax
def y = ay
def z = az
override def toString = "<"+x+", "+y+", "+z+">"
def add(that: Vector3[T]) = new Vector3(x+that.x, y+that.y, z+that.z)
}
The problem is I keep getting this error:
error: type mismatch;
found : T
required: String
def add(that: Vector3[T]) = new Vector3(x+that.x, y+that.y, z+that.z)
I've tried commenting out the "toString" method above, but that doesn't seem to have any effect. Can anyone tell me what I'm doing wrong?