更高级的类型推断

Higher-kinder types inference

(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 调用推断正确的最不常见超类型?

创建了一个 ticket,它被关闭为 "out of scope" 引用 Adriaan Moors:

Type inference does not support bounds that refer to other type parameters.