Java 泛型:通配符类型的编译错误
我已经切换到Java7Build21,开始出现奇怪的编译错误。例如,下面的代码段不会编译(尽管IntelliJ没有显示任何错误): 然后编译成功 我得到的错误有点神秘:Java 泛型:通配符类型的编译错误,java,generics,guava,java-7,Java,Generics,Guava,Java 7,我已经切换到Java7Build21,开始出现奇怪的编译错误。例如,下面的代码段不会编译(尽管IntelliJ没有显示任何错误): 然后编译成功 我得到的错误有点神秘: error: method transform in class Iterables cannot be applied to given types; required: Iterable<F>,Function<? super F,? extends T> found: Iterable<CA
error: method transform in class Iterables cannot be applied to given types;
required: Iterable<F>,Function<? super F,? extends T>
found: Iterable<CAP#1>,Function<CAP#2,String>
reason: no instance(s) of type variable(s) F,T exist so that argument type Function<CAP#2,String>
conforms to formal parameter type Function<? super F,? extends T>
where F,T are type-variables:
F extends Object declared in method <F,T>transform(Iterable<F>,Function<? super F,? extends T>)
T extends Object declared in method <F,T>transform(Iterable<F>,Function<? super F,? extends T>)
where CAP#1,CAP#2 are fresh type-variables:
CAP#1 extends Object from capture of ?
CAP#2 extends Object from capture of ? extends Object
错误:类Iterables中的方法转换无法应用于给定类型;
必需:Iterable,Function这是因为Function,
您只知道Iterable会吐出对象,因此您只能使用函数来转换它。方法签名是:
<F,T> Iterable<T> transform(Iterable<F> fromIterable, Function<? super F,? extends T> function)
Iterable变换(Iterable from Iterable,FunctionI实际上能够通过改变
2 Function<?, String> function = new Function<Object, String>() {
2函数这并不能解释为什么它在构建4中工作。也不能解释为什么Function@mindas显然,一个微妙的通配符捕获错误已经修复。而且,?extensed Object
对于我们来说与?
是相同的,因此同样的推理也适用。@mindas如果它在构建4中起作用,那么我怀疑这是真的y build 4中有一个bug。我很确定这是不应该编译的。请注意,?超级对象
只能是对象
…这与简单的注释非常不同。请注意,将刚刚创建的函数
分配给函数没有任何价值
2 Function<? extends Object, String> function = new Function<Object, String>() {
<F,T> Iterable<T> transform(Iterable<F> fromIterable, Function<? super F,? extends T> function)
Iterable<F> :
F can be any type here, but will influence the type parameter to Function
Function<? super F, ? extends T> :
the first type parameter (? super F) MUST be a supertype of F
Iterable<?>
Function<?, String>
2 Function<?, String> function = new Function<Object, String>() {
2 Function<? super Object, String> function = new Function<Object, String>() {