Implementing binary operations on generic class in scala -
i having multiple compile errors trying write class definition this.
trait binops[t] { def +(that: vector[t]): vector[t] def -(that: vector[t]): vector[t] def *(that: vector[t]): vector[t] def /(that: vector[t]): vector[t] } trait vector[t] { def tolist(): list[t] def zeros(length: int): vector[t] } object vector { def apply[t](args: t*): vector[t] = new vectorimpl[t](args.tolist) private class vectorimpl[@specialized(double, int, float, long)t](val _data: list[t]) extends vector[t] binops[t] { def +(that: vector[t]): vector[t] = new vectorimpl[t](_data.zip(that).map(elem => elem._1 + elem._2)) def -(that: vector[t]): vector[t] = new vectorimpl[t](_data.zip(that).map(elem => elem._1 - elem._2)) def *(that: vector[t]): vector[t] = new vectorimpl[t](_data.zip(that).map(elem => elem._1 * elem._2)) def /(that: vector[t]): vector[t] = new vectorimpl[t](_data.zip(that).map(elem => elem._1 / elem._2)) def fill(length: int, value: t): vector[t] = new vectorimpl[t](list.fill[t](length)(value)) def tolist(): list[t] = _data.tolist } implicit def vectortolist[t](v: vector[t]): list[t] = v.tolist }
i getting errors these..
error:(36, 102) type mismatch; found : t required: string def +(that: vector[t]): vector[t] = new vectorimpl[t](_data.zip(that).map(elem => elem._1 + elem._2)) ^ error:(37, 95) value - not member of type parameter t def -(that: vector[t]): vector[t] = new vectorimpl[t](_data.zip(that).map(elem => elem._1 - elem._2)) ^
however doing zip should have resulted in tuple type t , trying access first , second elements. doing wrong ?
error:(37, 95) value - not member of type parameter t
this real error (the 1 strings unfortunate consequence of implicit conversion in predef
- recommend building -yno-predef
avoid such misleading errors). t
unconstrained - -
supposed do? e.g. if t
locale
- locale.english - locale.spanish
? doesn't compile.
Comments
Post a Comment