« Return to Thread: [scala] vector math parameterized class in scala

Re: [scala] vector math parameterized class in scala

by Marcelo Fukushima :: Rate this Message:

Reply to Author | View in Thread

i think the problem is that Number does not define '+' and '*' methods
- and all the compiler knows about your T is that its a Number
(java.lang.Number)


On Tue, Jul 7, 2009 at 7:40 PM, Jonathan Bachrach<jrb@...> wrote:

> i'm trying to define a vector math class using parameterized classes in
> scala and am having trouble.  here's an abbreviated version of what i wrote:
> class Vec3[T <: Number] (xa: T, ya: T, za: T) {
>   val x: T = xa; val y: T = ya; val z: T = za
>   def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
>   def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
>   def len (): T = x + y + z
>   def == (o: Vec3[T]): Boolean = x == o.x && y == o.y && z == o.z
>   def < (o: Vec3[T]): Boolean = x < o.x && y < o.y && z < o.z
> }
> and here's what scalac says:
> rala-692> scalac vec3.scala
> vec3.scala:3: error: type mismatch;
>  found   : T
>  required: String
>   def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
>                                                ^
> vec3.scala:3: error: type mismatch;
>  found   : T
>  required: String
>   def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
>                                                         ^
> vec3.scala:3: error: type mismatch;
>  found   : T
>  required: String
>   def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
>                                                                  ^
> vec3.scala:4: error: value * is not a member of T
>   def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
>                                      ^
> vec3.scala:4: error: value * is not a member of T
>   def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
>                                             ^
> vec3.scala:4: error: value * is not a member of T
>   def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
>                                                    ^
> vec3.scala:5: error: type mismatch;
>  found   : T
>  required: String
>   def len (): T = x + y + z
>                       ^
> vec3.scala:7: error: value < is not a member of T
>   def < (o: Vec3[T]): Boolean = x < o.x && y < o.y && z < o.z
>                                   ^
> 8 errors found
> i had added the T <: Number to try to make it so that the + would be
> compatible but it still thinks + only works with strings.  i'm not even
> completely sure that Number is a real Scala Class.  i'm obviously not
> understanding how to write parameterized classes in scala.  any help would
> be appreciated.
>



--
http://mapsdev.blogspot.com/
Marcelo Takeshi Fukushima

 « Return to Thread: [scala] vector math parameterized class in scala