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

Re: [scala] vector math parameterized class in scala

by Kevin Wright-4 :: Rate this Message:

Reply to Author | View in Thread

You've also missed out on case classes and type inference, although this isn't causing your problem...

Using view bounds with the implicit conversions in Numeric should do the trick!

case class Vec3[T <:% Numeric] (x: T, y: T, z: T) {
  def + (o: 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 = x + y + z 
  def == (o: Vec3[T]) = x == o.x && y == o.y && z == o.z 
  def < (o: Vec3[T]) = x < o.x && y < o.y && z < o.z 
}

On Tue, Jul 7, 2009 at 11:59 PM, Jorge Ortiz <jorge.ortiz@...> wrote:
Take a look at Scala 2.8's Numeric trait:

http://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/src/library/scala/Numeric.scala

--j


On Tue, Jul 7, 2009 at 5:56 PM, Jonathan Bachrach <jrb@...> wrote:
i see.  that's unfortunate.  how would you recommend writing a vector math parameterized class in scala then (short of actually copying the code)?


On Jul 7, 2009, at 3:50 PM, Marcelo Fukushima wrote:

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