Comparators.comparing inference error

Michael Nascimento misterm at gmail.com
Thu Apr 18 11:18:24 PDT 2013


Hi folks,

I had a comparison chain in Guava which I tried to migrate by creating
a comparator:

Comparator<YearMonthDecade> c = Comparators
.comparing(YearMonthDecade::getYearMonth)
.thenComparing(YearMonthDecade::getDecadeNumber);

getYearMonth returns a YearMonth and getDecadeNumber returns an int. I
get the following compiler error:

YearMonthDecade.java:[69,4] error: reference to comparing is ambiguous
  both method <T#1>comparing(ToLongFunction<? super T#1>) in
Comparators and method <T#2>comparing(ToDoubleFunction<? super T#2>)
in Comparators match
  where T#1,T#2 are type-variables:
    T#1 extends Object declared in method
<T#1>comparing(ToLongFunction<? super T#1>)
    T#2 extends Object declared in method
<T#2>comparing(ToDoubleFunction<? super T#2>)
YearMonthDecade.java:[69,14] error: incompatible types: Cannot
instantiate inference variables T because of an inference loop

I can get it to compile with:

Comparator<YearMonthDecade> c = Comparators
.<YearMonthDecade,YearMonth>comparing(YearMonthDecade::getYearMonth)
.<YearMonthDecade>thenComparing((ToIntFunction<YearMonthDecade>)
YearMonthDecade::getDecadeNumber);

Do you plan to make this work somehow (smarter compiler or no
overloading anymore)?

Regards,
Michael


More information about the lambda-dev mailing list