I created a class that can be parameterized by anyone that can be converted to numeric
class Complex[T <% Numeric[T]] (val real : T, val imag : T) { //... complex number methods ... }
Then in another place in the code I try:
var myComplex = new Complex(0, 1)
This causes a compilation error because (surprisingly) there is 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 don't see?
There is an implicit object called IntIsIntegral defined in Numeric.scala. I tried using this to create my own implicit conversion method:
def implicit intToNumericInt(val i : Int)(implicit n : IntIsIntegral) = n.fromInt(i)
I am surprised that this is necessary and, in any case, this leads to infinite recursion into the .fromInt method.
I am sure that I am missing something basic (as you can tell, I am new to Scala), so I would rate the point in the right direction.
As you can see from the example, I'm trying to get a complex number job that can accept and work with any numeric type. I hope to contribute to the scalala (linear algebra) project. After that, I want to introduce "Titt", which describes the responsibilities of the elements in the matrix (basically, just the + and * operators) and the re-equipment of support for complex numbers in the matrix manipulation library.
source share