Lambda conversion in Method invocation context

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Fri Nov 12 01:28:24 PST 2010


On 12/11/10 07:11, Peter Levart wrote:
> On 11/12/10, Arul Dhesiaseelan wrote:
>    
>> Hi,
>>
>> I get the following compile error when I use a lambda in a method invocation
>> context:
>>
>> SimpleRunnable.java:14: internal error; cannot instantiate println(String)
>> at ExecutorService to (<>#void())
>>                  executor.submit(#{System.out.println("Processing a
>> short-lived asynchronous task.")});
>>                                 ^
>> 1 error
>>
>>      
> I think, you're trying to write this:
>
> executor.submit(
>    #{ System.out.println("Processing a short-lived asynchronous task."); }
> );
>
> ...note the semicolon after println method invocation. Invocation of a void method can not be used as expression.
>
> Javac error is somewhat strange though.
>
> Peter
>
>    
The semi-colon should not represent a problem. The real problem is that 
the call is ambiguous: ExecutorService defines two methods called 
'submit', one accepting Callable(T) and another one accepting a 
Runnable. Both target methods are compatible with a lambda whose type is 
'void()'.

The error message is weird because the prototype has some problems 
related to error recovery during overload resolution (which I'm trying 
to fix) - the version I'm working on emits this error:

/home/maurizio/Desktop/Inference.java:6: reference to submit is 
ambiguous, both method <T>submit(Callable<T>) in ExecutorService and 
method submit(Runnable) in ExecutorService match
e.submit(#{System.out.println("Processing a short-lived asynchronous 
task.")});//fails
  ^
   where T is a type-variable:
     T extends Object declared in method <T>submit(Callable<T>)
1 error

Which does a better job at explaining what's wrong.

Maurizio


More information about the lambda-dev mailing list