Let's say I have this:
trait Animal {
type Species
}
I can write quite easily a function that accepts only two animals of the same species
def breed(a: Animal, b: Animal)(implicit evidence: a.Species =:= b.Species) = ???
but I want to create a class with the same restriction:
class Bed(a: Animal, b: Animal)(implicit evidence: a.Species =:= b.Species)
but it will not compile. I tried several combinations of trying to use traits with stable identifiers and restrictions and what not, but no matter what I do, I seem to always get problems
trait Bed {
type T
def a: Animal { type Species = T }
def b: Animal { type Species = T }
}
object Bed {
def apply(a1: Animal, b1: Animal)(implicit ev: a1.Species =:= b1.Species) = new Bed {
type T = b1.Species
def a = a1 // this line won't compile, as the compiler can't see the two species are equal ?
def b = b1
}
}
Thank.
source
share