The following code demonstrates it clearly:
trait Poly1Group[-IUB, +OUB] {
trait Case[-I <: IUB] {
type Out <: OUB // <------------------------------- success
def apply(v: I): Out
}
trait ==>[
-I <: IUB,
O <: OUB //<------------------------------------- fail
] extends Case[I] {
final type Out = O
}
def apply[I <: IUB](v: I)(implicit ev: Case[I]): ev.Out = ev(v)
}
It should be noted that both Out
and O
are invariant, so they should impose no constraint on the variance of OUB. Yet one succeed and one fails:
[Error] .../Poly1Group.scala:16: covariant type OUB occurs in contravariant position in type <: OUB of type O
one error found
How could it be possible?
question from:
https://stackoverflow.com/questions/65944627/in-scala-how-could-a-covariant-type-parameter-be-an-upper-bound-of-an-abstract 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…