Created
July 29, 2011 16:57
-
-
Save npryce/1114218 to your computer and use it in GitHub Desktop.
Java Generics Hell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? | |
} | |
} |
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
Not fixed in Java 7