History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: IDEA-18539
Type: Bug Bug
Status: Open Open
Assignee: Alexey Kudravtsev
Reporter: Eugene Kirpichov
Votes: 0
Watchers: 3
Operations

If you were logged in you would be able to see more operations.
IDEA: Feedback

Bad code is green: generics, "? super T"

Created: 24 Jun 08 14:57   Updated: 25 Aug 08 21:59
Component/s: Editor. Error Highlighting

Build: 7,860


 Description  « Hide
public class CollectionUtils {
    public static <T extends Comparable<? super T>> Comparator<T> comparableComparator() {
        return new Comparator<T>() {
            public int compare(T o1, T o2) {
                return o1.compareTo(o2);
            }
        };
    }

    public static <T> Comparator<T> reverseComparator(
            final Comparator<T> c) {
        return new Comparator<T>() {
            public int compare(T o1, T o2) {
                return -c.compare(o1, o2);
            }
        };
    }
                                                 
    public static <T extends Comparable<? super T>> Comparator<T> reverseComparableComparator() {
        return CollectionUtils.reverseComparator(CollectionUtils.<T>comparableComparator());
    }

    private static Object foo() {
        return new TreeMap<Integer, Object>(reverseComparableComparator());        // IDEA doesn't highlight an error, but javac does: incompatible types; inferred type argument(s) java.lang.Comparable<? super T> do not conform to bounds of type variable(s) T
    }


 All   Comments   Work Log   Change History      Sort Order:
Eugene Vigdorchik - 25 Aug 08 21:59
IDEA currently infers Comparator<Comparable>. Setting T -> ? instead of T -> null for failed inference from parent seems to make more sense and coincide with javac.
Though eclipse clearly does one more level of unfolding: it diagnoses Comparable<? super Comparable<? super T>>. Any way, this code will be legal if JSR to improve type inference will ever approve it