I've created a class that can be parameterised by anything that can be converted to Numeric
class Complex[T <% Numeric[T]] (val real : T, val imag : T) {
//... complex number methods ...
}
Then elsewhere in the code I try:
var myComplex = new Complex(0, 1)
This raises a compilation error because (surprisingly) there's no implicit conversion between Int and Numeric[Int] or even between Int and Integral[Int].
Am I missing something? Is there an implicit conversion somewhere I'm not seeing?
There's an implicit object called IntIsIntegral defined in Numeric.scala. I've tried using this to create my own implicit conversion method:
def implicit intToNumericInt(val i : Int)(implicit n : IntIsIntegral) = n.fromInt(i)
I'm surprised that this is required and, anyway, it seems to lead to an infinite recursion into the .fromInt method.
I'm sure that I'm missing something basic (as you can tell, I'm new to Scala) so would appreciate a point in the right direction.
As you can see from the example, I'm trying to get a Complex number implementation working which can accept and work with any Numeric type. I hope to contribute this to the scalala (linear algebra) project. Following that, I want to introduce a Trait which describes the responsibilities of elements in a matrix (mainly just + and * operators) and retrofit support for complex numbers into the matrix manipulation library.