2017-02-13 84 views
1

(斯卡拉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呼叫推断正确的最小公用超类型?

+1

不是一个答案,但你真的需要'T2'吗? 'def addAll [B>:A](that:Container [B]):Boolean'应该可以工作,更像你通常在这种情况下看到的。 –

+0

不幸的是,在实际的代码中,依赖于'T2'的类型约束,所以我不认为这是可能的。上面的代码片段是我在调查原始代码中的问题时提出的简化代码。 –

+0

实际上,你似乎断言'Container [Int] <:Container [B]'其中'B>:String'。如果您明确地将B设置为任意,它将起作用(如您所见),但我认为类型推断系统不允许将“任何”替换为“解决方案”。虽然我不是100%确定的。 – Phasmid

回答

0

创建一个ticket,将其关闭为“超出范围” 报价由Adriaan Moors

类型推断不支持指其它类型的参数界限。