说我有这个:
trait Animal {
type Species
}
我可以很容易地编写一个只需要两个相同物种的动物的函数
def breed(a: Animal, b: Animal)(implicit evidence: a.Species =:= b.Species) = ???
但我想创建一个具有相同约束的类:
class Bed(a: Animal, b: Animal)(implicit evidence: a.Species =:= b.Species)
但它不会编译。我尝试了一些尝试使用具有稳定标识符和约束的特征的组合,但无论我做什么 - 我似乎总是遇到问题
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
}
}
谢谢。