Loss of conciseness due to overload ambiguity

Remi Forax forax at univ-mlv.fr
Wed Jan 9 09:41:07 PST 2013


 From lambda-dev,
This is typically a case where you can see that transforming a method 
reference to an implicit lambda when doing inference is a bad idea. 
Instead of trying to tweak the algorithm if there is one applicable 
method, why not recognizing that this is done in the wrong way.

I should go that way,
   gather all applicable method reference i.e all method with a matching 
name whenever the number of parameters,
   then for each of them, use the signature of the method (e.g. Person 
-> String) to find the lambda descriptor
   (it's equivalent to transform it to an *explicitly typed* lambda and 
to have infered that the result type is the return type of the method)
  then if more than one method is applicable, try to select the best one.

Rémi

On 01/09/2013 05:45 PM, Maurizio Cimadamore wrote:
> We are considering ways to mitigate this; if the method reference is
> unambiguous (only one match) there are facts that can be used to achieve
> more overload resolution precision (pretty much what we do with implicit
> lambdas - where we use arity to prune unwanted candidates).
>
> Maurizio
>
> On 09/01/13 17:07, Venkat Subramaniam wrote:
>> Greetings,
>>
>> Is there a way to gain the desired conciseness in the following case?
>>
>>       List<Person> people = Arrays.asList(
>>         new Person("Kate", 10),
>>         new Person("Jack", 10)
>>       );
>>       
>>       Function<Person, String> byName = person -> person.getName();
>>
>>       people.stream().sorted(comparing(byName)).into(new ArrayList<>());
>>       //[Jack - 10, Kate - 10]
>>       
>>       people.stream().sorted(comparing(Person::getName)).into(new ArrayList<>());
>>         
>>       /*
>>         error: reference to comparing is ambiguous
>>             people.stream().sorted(comparing(Person::getName)).into(new ArrayList<>());
>>                                      ^
>>           both method <T#1>comparing(IntFunction<? super T#1>) in Comparators and method <T#2,U>comparing(Function<? super T#2,? extends U>) in Comparators match
>>           where T#1,T#2,U are type-variables:
>>             T#1 extends Object declared in method <T#1>comparing(IntFunction<? super T#1>)
>>             T#2 extends Object declared in method <T#2,U>comparing(Function<? super T#2,? extends U>)
>>             U extends Comparable<? super U> declared in method <T#2,U>comparing(Function<? super T#2,? extends U>)
>>         1 error
>>         
>>       */
>>
>> Thanks,
>>
>> Venkat
>>
>



More information about the lambda-spec-experts mailing list