Warum wird dies in Java7 kompiliert und nicht in Java8?
Generika sind knifflig. Und es sieht so aus, als würden sie in verschiedenen Java-Versionen unterschiedlich behandelt.
Dieser Code wird erfolgreich in Java 7 kompiliert und kann nicht mit Java 8 kompiliert werden.
import java.util.EnumSet;
public class Main {
public static void main(String[] args) {
Enum foo = null;
tryCompile(EnumSet.of(foo));
}
static <C extends Enum<C> & Another> void tryCompile(Iterable<C> i) {}
static interface Another {}
}
Hier ist eine Fehlermeldung von Java 8. Ich habe diese verwendet, um sie zu kompilieren:http: //www.compilejava.net
/tmp/java_A7GNRg/Main.java:6: error: method tryCompile in class Main cannot be applied to given types;
tryCompile(EnumSet.of(foo));
^
required: Iterable<C>
found: EnumSet
reason: inferred type does not conform to upper bound(s)
inferred: Enum
upper bound(s): Enum<Enum>,Another
where C is a type-variable:
C extends Enum<C>,Another declared in method <C>tryCompile(Iterable<C>)
/tmp/java_A7GNRg/Main.java:6: warning: [unchecked] unchecked method invocation: method of in class EnumSet is applied to given types
tryCompile(EnumSet.of(foo));
^
required: E
found: Enum
where E is a type-variable:
E extends Enum<E> declared in method <E>of(E)
1 error
1 warning
Die Frage bezieht sich auf den Unterschied zwischen Versionen des Java-Compilers.