Skip to content

Instantly share code, notes, and snippets.

@npryce
Created July 29, 2011 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save npryce/1114218 to your computer and use it in GitHub Desktop.
Save npryce/1114218 to your computer and use it in GitHub Desktop.
Java Generics Hell
import java.util.Comparator;
public class GenericsHell {
public static void compareSomeNumbers(Comparator<Integer> numberComparison) {
// ... elided ...
}
public static <T> Comparator<T> arbitraryOrder() {
return new Comparator<T>() {
@Override
public int compare(T o1, T o2) {
return 0;
}
};
}
public static void assignToVariable() {
Comparator<Integer> comparator = arbitraryOrder();
compareSomeNumbers(comparator);
}
public static void assignToParameter() {
compareSomeNumbers(arbitraryOrder()); // Why does this not compile?
}
}
@npryce
Copy link
Author

npryce commented Jul 31, 2011

Not fixed in Java 7

@Fuud
Copy link

Fuud commented Aug 13, 2011

@npryce
Copy link
Author

npryce commented Sep 20, 2011

Note: although closed as not a defect, the comment says that it is only not being considered a defect because it's too difficult to change the compiler code to implement it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment