海峡<代码>
trait T {
def v: Int
def +(t: T): T
}
缩略语
case class A(v: Int) extends T {
def +(a: A) = A(v + a.v)
}
is not a valid subtype of T
. The implementation of A.+
is too restrictive, because it only accepts elements of type A
whereas the signature of T.+
requires all implementations to be able to accept objects of type T
and not just objects of type A
.
So far, so reasonable.
如果我允许实施<代码>T,则限制性的I可修改<代码>A
的声明如下:
trait T[This <: T[This]] {
def v: Int
def +(t: This): This
}
case class A(v: Int) extends T[A] {
def +(a: A) = A(v + a.v)
}
这显然炸毁了签字类型。
是否有另一种方式宣布实施<代码> T 只需要与自己类型的物体相容?
1st EDIT http://stackoverflow.com/questions/4438507/abstract-classes-traits-and-invariant-Functions/4438883#4438883" 页: 1
虽然自发型的确缩短了目前的签名,但它们并没有缩短出现<代码>T的其他签名,例如:
trait C[D <: T[D], S] { self: S =>
def +(perm: D): S
def matches(other: S): Boolean
}