高阶类型推断

亚历克斯·阿卜杜加法罗夫(Alex Abdugafarov)

(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调用不能推论正确的最小公共超类型?

亚历克斯·阿卜杜加法罗夫(Alex Abdugafarov)

创建了一张票证,该票证由于“超出范围”而关闭,由Adriaan Moors引用

类型推断不支持引用其他类型参数的范围。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章