繁体   English   中英

为什么java推断失败

[英]why does java inference fail

我有以下看似类似的方法, do1do2

class Demo<A>{
    public <C> Iterable<C> do1(List<? super C> _a) {
        return null;
    }


    public <C extends D, D> Iterable<C> do2(List<D> _a) {
        return null;
    }

    {
        List<? extends A> leA = null;

        do2(leA);
        do1(leA);
    }
}

当我编译上面的代码(javac 1.8.0_92)时,调用do1(leA) do2(leA)工作,而do1(leA)失败。

required: List<? super C>
found: List<CAP#1>
reason: cannot infer type-variable(s) C
(argument mismatch; List<CAP#1> cannot be converted to List<? super C>)
where C,A are type-variables:
  C extends Object declared in method <C>do1(List<? super C>)
  A extends Object declared in class Cache
where CAP#1 is a fresh type-variable:
  CAP#1 extends A from capture of ? extends A

现在我想知道:这是由于javac中类型推断的实现不完整,还是我通过调用do1(leA)创建了一个无效的类型树?

据我所知:

  • do1(leA)Capture(? extends A)成为C的超类型
  • do2(leA)Capture(? extends A)成为C的超类型(间接通过: Capture(? extends A) == DD :> C

意味着在两种情况下C应该(没有错误)解析为"? extends A"

你需要extends C而不是super C

public <C> Iterable<C> do1(List<? extends C> _a) {

super C是和祖先,而不是子类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM