Say I have this:
trait Animal {
type Species
}
I can easily enough write a function that only takes 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 sort of constraint:
class Bed(a: Animal, b: Animal)(implicit evidence: a.Species =:= b.Species)
but it won't compile. I've tried a few combinations of trying to use traits with stable identifiers and constraints and what not, but no matter what I do -- I seem to always end up with 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
}
}
Thanks.