I'm writing a math library for complex vector computing, part of which looks like this:
object Example1 {
val two = 2
val three = 3
// SU means 'special unitary group'
trait SU_n[D <: Int] {
def plus(v1: ComplexVector[D], v2: ComplexVector[D])(
implicit ev: D =:= two.type
): ComplexVector[D] = {
//TODO: some unspeakable magic here
???
}
}
class ComplexVector[D <: Int: ClassTag](xyzw: List[(Double, Double)]) {
{
assert(xyzw.size.isInstanceOf[D])
}
}
type Quaternion = ComplexVector[two.type]
val alsoTwo = 2
object SU_2 extends SU_n[two.type] {}
object SU_Also2 extends SU_n[alsoTwo.type] {}
object SU_3 extends SU_n[three.type] {}
val q = new Quaternion(List(1.0 -> 2.0, 3.0 -> 4.0))
{
val v2 = SU_2.plus(q, q)
val also_v2 = SU_Also2.plus(q, q)
}
val vec =
new ComplexVector[three.type](List(1.0 -> 2.0, 3.0 -> 4.0, 5.0 -> 6.0))
// This will break
// {
// val v3 = SU_3.plus(vec, vec)
// }
}
the infix type =:= is used to ensure that function plus won't be usable on vectors other than quaternions, when I compile it, I got the following error message:
Error: type mismatch;
found : <mypackage>.Example1.two.type (with underlying type Int)
required: AnyRef
type Quaternion = ComplexVector[two.type]
The strange thing is that I can't find anywhere in the implementation of infix class =:= that mandates its operand to be AnyVal, so why I'm getting this error? And how to fix/bypass it to achieve the requirement? (namely, create a function that can only be applied on quaternion)