I want to be able to implicitly convert Tuples of numbers (Ints and double) into a vector object.
Assuming a Vector class with a + method
case class Vector(x: Double, y:Double){
def + (v:Vector)= new Vector(x+v.x,y+v.y)
}
My goal is to have the following code work.
val vec = (1,2)+(.5,.3) // vec == Vector(1.5,2.3)
I can get it to work for Int
with the following
implicit def int2vec(t:Tuple2[Int,Int])=new Vector(t._1,t._2)
val vec = (1,2)+(3,4) // vec == Vector(4.0,6.0)
But it fails when I add the convertion for double
implicit def int2vec(t:Tuple2[Int,Int])=new Vector(t._1,t._2)
implicit def double2vec(t:Tuple2[Double,Double])=new Vector(t._1,t._2)
val a = (1,2)
val b = (.5,.3)
val c = (1,1)+b // vec = Vector(1.5,1.3)
val d = (1,2)+(.3,.5) // compile error: wrong number of arguments
val e = (1,2)+((.3,.5)) // compile error: type mismatch
Trying just double per Andri's sugestion
implicit def double2vec(t:Tuple2[Double,Double])=new Vector(t._1,t._2)
val a = (.5,.3)
val b = (1,1)+a // type mismatch found:(Double,Double) required:String
What do I need to do to get this to work?