Type parameterization in Scala
Posted
by horatius83
on Stack Overflow
See other posts from Stack Overflow
or by horatius83
Published on 2010-05-14T18:48:46Z
Indexed on
2010/05/14
18:54 UTC
Read the original article
Hit count: 239
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?
© Stack Overflow or respective owner