I am trying to implement a generic Scala method that processes Breeze vectors typed as Float or as Double (at least, less specificity a plus). Here is a simple example for Vector[Double]:
def vectorSum(vectors: Seq[Vector[Double]]): Vector[Double] = {
vectors.reduce { (v1, v2) => v1 :+ v2 }
}
I am slightly new to Scala and Breeze, so my naive approach to make this generic is:
def vectorSumGeneric[T <: AnyVal](vectors: Seq[Vector[T]]): Vector[T] = {
vectors.reduce { (v1, v2) => v1 :+ v2 }
}
However, this throws the following compile errors:
diverging implicit expansion for type breeze.linalg.operators.OpAdd.Impl2[breeze.linalg.Vector[T],breeze.linalg.Vector[T],That] starting with method v_v_Idempotent_OpAdd in trait VectorOps
not enough arguments for method :+: (implicit op: breeze.linalg.operators.OpAdd.Impl2[breeze.linalg.Vector[T],breeze.linalg.Vector[T],That])That. Unspecified value parameter op.
I've tried with some variations including T <% AnyVal
and T <% Double
, but they do not work either (as expected, probably). The Scala documentation to type bounds do not give me a clue about such a use case such as this.
What is the correct way to solve this?