ambiguous type inference while working with primitives

Antoras mail at antoras.de
Tue Mar 6 12:51:26 PST 2012


I get compiler errors due to ambiguous Mapper-objects. I have a class 
Person which has a method getAge(). If the return value of this method 
is 'int' the following inline Mapper-object produces an error:

Comparator<Person> c = comparing((Person p1) -> p1.getAge());

If the return type is 'Integer' the above code works.

This line:

Comparator<Person> c = comparing(Person::getAge);

works neither with 'Integer' nor with 'int';

Is this expected behavior or should the type checker recognize that 
IntMapper is the first one to "choose"?


The error message:

ListTest.java:203: error: reference to comparing is ambiguous, both 
method <T#1>comparing(LongMapper<T#1>) in Comparators and method 
<T#2>comparing(IntMapper<T#2>) in Comparators match
     Comparator<Person> c = comparing((Person p1) -> p1.getAge());
                            ^
   where T#1,T#2 are type-variables:
     T#1 extends Object declared in method <T#1>comparing(LongMapper<T#1>)
     T#2 extends Object declared in method <T#2>comparing(IntMapper<T#2>)


More information about the lambda-dev mailing list