Наконец-то нашел ответ :-) Думаю, я не так уж далек с первой попытки.Вот оно: (написано для scala 2.8)
trait MatrixElement[T] {
def +(that: T): T
def *(that: T): T
}
object MatrixElement {
implicit def intToMatrixElement(x : Int) = new MatrixElement[Int] {
def +(y : Int) = x + y
def *(y : Int) = x * y
}
implicit def doubleToMatrixElement(x : Double) = new MatrixElement[Double] {
def +(y : Double) = x + y
def *(y : Double) = x * y
}
implicit def complexToMatrixElement(x : Complex) = new MatrixElement[Complex] {
def +(y : Complex) = x + y
def *(y : Complex) = x * y
}
}
class Matrix[T <% MatrixElement[T] : ClassManifest ](d: Array[Array[T]]) {
def *(that: Matrix) = ..// code that uses "+" and "*" on the elements
}
Теперь я могу делать такие вещи, как:
scala> new Matrix(Array(Array(1,0),Array(0,1)))
res0: Matrix[Int] =
1 0
0 1
scala> new Matrix(Array(Array(new Complex(0),new Complex(1)),Array(new Complex(1),new Complex(0))))
res9: Matrix[Complex] =
(0.0,0.0i) (1.0,0.0i)
(1.0,0.0i) (0.0,0.0i)