overload resolution with deep nested lambda

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Thu Sep 11 15:05:29 UTC 2014


I think the latest version of the compiler (JDK 9) has been fixed - it 
now gives this:

/home/maurizio/Desktop/Main.java:6: error: incompatible types: cannot 
infer type-variable(s) O
         List<Optional<Function<String, String>>> list = 
asList(of((String s) -> identity(s)));
                                                               ^
     (argument mismatch; Object is not a functional interface)
   where O is a type-variable:
     O extends Object declared in method <O>of(O)


The message is a bit cryptic - but the compiler now obviously chooses 
asList(Optional), hence the target-type for the lambda is just Object 
(since Optional is raw).

Maurizio

On 11/09/14 15:26, anna.kozlova at jetbrains.com wrote:
> the part "A method is applicable if it is either applicable by 
> subtyping (§15.12.2.2), applicable by method invocation conversion 
> (§15.12.2.3), or it is an applicable variable arity method 
> (§15.12.2.4)." is from JLS 7, JLS 8 doesn't say so anymore.
>
> I believe that (String s) -> identity(s) is not pertinent to 
> applicability because it is passed to the parameter with type T 
> (15.12.2.2) so it should not participate in invocation applicability 
> inference (18.5.1).
>
> Thank you
> Anna
>
> On 11.09.2014 17:56, Davin McCall wrote:
>> On 10/09/14 19:26, anna.kozlova at jetbrains.com wrote:
>>>
>>> Because of((String s) -> identity(s)) is potentially compatible with 
>>> both methods (15.12.2.1), method applicability search yields 2 
>>> methods applicable by strict invocation, of which asList(Optional a) 
>>> is more specific (according to 18.5.4) but javac obviously chooses 
>>> asList(T).
>>>
>>> Could someone explain what have I missed here please?
>>
>> I'm no expert here, but I believe that 15.12.2.1 identifies
>> _potentially) applicable methods, which are then filtered according to
>> 15.12.2.2, 15.12.2.3 and 15.12.2.4.
>>
>> From 15.12.2: "A method is applicable if it is either applicable by
>> subtyping (§15.12.2.2), applicable by method invocation conversion
>> (§15.12.2.3), or it is an applicable variable arity method
>> (§15.12.2.4)."
>>
>> This removes the possibility of using asList(Optional), because no
>> suitable type can be inferred for O. The only remaining option is
>> asList(T).
>>
>> Davin
>>
>>
>>
>> !DSPAM:35,5411aafa47141724266025!
>



More information about the lambda-dev mailing list