(Scala 2.11.8)
请考虑以下代码段:
class Case2 {
trait Container[+A] {
def addAll[B >: A, T2 <: Container[B]](that: T2): Boolean
}
def t1: Container[String] = ???
def t2: Container[Int] = ???
// Works
t1.addAll[Any, Container[Any]](t2)
// Errors:
//* type mismatch; found : Case2.this.Container[Int] required: T2
//* inferred type arguments [String,Case2.this.Container[Int]] do not conform to method addAll's type parameter bounds [B >: String,T2 <: Case2.this.Container[B]]
t1.addAll(t2)
}
为什么不能将addAll
调用推断作为适当的最不常见的超类型?