我有:
trait Pet[T <: Pet[T]] { //disallows: Dog extends Pet[String]
self: T => //disallows: Dog extends Pet[Monkey]
def rename(s: String): T
def name: String
}
现在可以很容易地添加像Feline
这样可以扩展Pet
类的特征,如下所示:
trait Feline[T <: Feline[T]] extends Pet[T] {
self: T =>
def pur : Unit = println("purrrr")
def scratch: Unit
}
但是,如果我要引入一种Pet
与自我类型混合的类型,例如:
trait PetCareInfo[T <: PetCareInfo[T]] {
self: T with Pet[T] =>
def registerPet: Unit
}
我得到错误:
类型参数 [T] 不符合 trait Pet 的类型参数边界 [T <: Pet[T]]
我的理解是,这是因为自我类型检查会单独查看PetCareInfo
类型A with B
,因此无法满足限制。(不确定这是错误还是功能)
我可以改用存在类型:
type TypeRestriction: Pet[A] forSome {type A}
trait PetCareInfo[T <: PetCareInfo[T]] {
self: T with TypeRestriction => //mix-in restriction
def registerPet: Unit
}
这会有点工作。两个问题:
- 我无法在混入限制行直接定义存在类型。我得到:
; 预期但找到了“forSome”。
有没有办法解决这个问题?
在实践中
PetCareInfo
'forSome
限制 +Pet
自己的限制意味着我不能拥有:class Cat extends Pet[Dog] with PetCareInfo[Cat]
但我想知道是否有办法不依赖Pet
于此。
更新:
对于问题 2,我可以将现有的类型限制更改为:
type Restriction[T] = A with Pet[A] forSome {type A <: PetCareInfo[T]}
trait PetCareInfo[T <: PetCareInfo[T]] {
self: Restriction[T] =>
def registerPet: Unit
}
这似乎正在解决问题。虽然,仍然不能保证结构A
类型与 相同T
,所以我们仍然依赖Pet
. :(