我已經切換到Java 7構建21並開始得到奇怪的編譯錯誤。例如,下面的代碼片段不編譯(儘管的IntelliJ不顯示任何錯誤):泛型:與通配符類型編譯錯誤
1 Iterable<?> parts = ImmutableList.<String>of("one", "two");
2 Function<?, String> function = new Function<Object, String>() {
3 @Override
4 public String apply(final Object input) {
5 return input.toString();
6 }
7 };
8 Iterable<String> result = Iterables.transform(parts, function);
9 System.out.println(result);
但如果我在第2行替換?
到Object
2 Function<Object, String> function = new Function<Object, String>() {
然後編譯成功。
我得到的錯誤是略帶神祕:
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
更改線2是
2 Function<? extends Object, String> function = new Function<Object, String>() {
沒有效果。
我正在使用JDK 1.7.0_11-b21;這用於編譯確定構建4.
這是一個javac
錯誤或我的?
請注意,將剛剛創建的'Function