Assuming that I'm writing a program for vector multiplication. Following the requirement in this article:
https://etrain.github.io/2015/05/28/type-safe-linear-algebra-in-scala
The multiplication should only compile successfully if the dimension of both vectors are equal. For this I define a generic type Axis that uses a shapeless literal type (the number of dimension) as the type parameter:
import shapeless.Witness
trait Axis extends Serializable
case object UnknownAxis extends Axis
trait KnownAxis[W <: Witness.Lt[Int]] extends Axis {
def n: Int
def ++(that: KnownAxis[W]): Unit = {}
}
object KnownAxis {
val w1 = Witness(1)
val w2 = Witness(2)
case class K1(n: Witness.`1`.T) extends KnownAxis[w1.type]
case class K2(n: Witness.`2`.T) extends KnownAxis[w2.type]
// K2(2) ++ K1(1) // doesn't compile
K2(2) ++ K2(2)
}
So far so good, the problem however appears when I try to generalise it for all n:
case class KN[W <: Witness.Lt[Int]](n: W#T) extends KnownAxis[W]
KN(1)
The above code triggers a compilation error:
Axis.scala:36: type mismatch;
found : Int(1)
required: this.T
[ERROR] KN(1)
[ERROR] ^
[ERROR] one error found
My question is: why Spark is incapable of focusing on the more refined type Witness.`1`.T, instead it is using type Int? What does it take to override this behaviour so case class KN can be successfully defined?
UPDATE 1: The follow up has been moved to a new question: