I am trying to enforce a rule that the (dependent) return type of a typeclass, must itself implement a typeclass. So when the user implements the IsVec
typeclass below, they must also ensure that the return value of the getElem
method implements another typeclass (IsVecElem
). My attempts to make this work look something like this:
// A typeclass for an vector element
abstract class IsVecElem[A, T: Numeric] {
def dataOnly(self: A): T
}
// A typeclass for a vector
abstract class IsVec[A, T: Numeric] {
// I would like this OutElem output type to implement the IsVecElem typeclass
protected type OutElem
def getElem(self: A, i: Int)(implicit tcO: IsVecElem[OutElem, T]): OutElem
}
// give this typeclass method syntax
implicit class IsVecOps[A, T: Numeric](value: A) {
def getElem(i: Int)(implicit tcA: IsVec[A, T], tcO: IsVecElem[tcA.OutElem, T]) = tcA.getElem(value, i)
}
The problem comes with getElem in IsVecOps - this raises a compile error:
type OutElem in class IsVec cannot be accessed as a member of IsVec[A, T] from class IsVecOps. Access to protected type OutElem not permitted because enclosing class IsVecOps is not a subclass of class IsVec
Having IsVecOps
extend IsVec
isn't an immediate solution and doesn't feel like it should be, so I'm wondering if there's an error in approach elsewhere in the code.
Any help much appreciated.