Wou*_*ter 6 java type-inference javac
我有以下看似相似的方法,do1并且do2:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
当我编译上面的代码(javac的1.8.0_92),呼吁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
Run Code Online (Sandbox Code Playgroud)
现在我想知道:这是因为javac中类型推断的实现不完整,还是我在调用时创建了一个无效的类型树do1(leA)?
据我所知:
do1(leA):Capture(? extends A)成为C的超类型do2(leA):Capture(? extends A)成为C的超类型(间接通过:Capture(? extends A) == D和 D :> C在两种情况下C都应该(没有错误)解决"? extends A"
Old*_*eon -2
你extends C不需要super C:
public <C> Iterable<C> do1(List<? extends C> _a) {
Run Code Online (Sandbox Code Playgroud)
A superofC是祖先,而不是子类。