Latest build c572fa185d05 limitations

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Tue Sep 14 08:53:44 PDT 2010


On 13/09/10 23:49, Peter Levart wrote:
> On 09/14/10, Howard Lovatt wrote:
>    
>> I am contrasting:
>>
>>    il.forEach( #( int i ) { 2 * i } );
>>
>> which fails, however:
>>
>>    il.forEach( #( int i ) {
>>       if ( i<   4 ) { throw new Exception(); }
>>       return i * i;
>>     } );
>>
>> Works and yet both are #(int i) { ... }.
>>
>> I also think people are used to interchanging Integer and int and
>> expect that boxing to take care of the details. So I think having to
>> say Integer will be confusing.
>>      
> In this particular case, I think that mandating the lambda parameter to be "Integer" instead of allowing it to be "int" is a better choice. Automatically un-boxing the Integer would throw NPE whenever null is passed to a SAM instance. You can use the "Integer" variable (parameter) in the lambda body wherever an "int" variable fits and any NPEs thrown at the parameter use sites at least have their line numbers.
>    
The compiler should mandate Integer - the fact that (sometimes) it is 
allowing 'int' is a bug - I will fix it shortly.

Maurizio
> Regards, Peter
>
>    
>> Cheers,
>>
>>   -- Howard.
>>
>> On 14 September 2010 13:47, Maurizio Cimadamore
>> <maurizio.cimadamore at oracle.com>  wrote:
>>      
>>> On 12/09/10 21:38, Howard Lovatt wrote:
>>>        
>>>> It is odd that Integer works and int doesn't, the programmer will expect
>>>> that they are interchangeable. Even stranger is that:
>>>>
>>>>    il.forEach( #( int i ) {
>>>>      if ( i<   4 ) { throw new Exception(); }
>>>>      return i * i;
>>>>    } );
>>>>
>>>>          
>>> The problem here is that you are passing a lambda of the kind #(int x) where
>>> a Sam is expected, where the Sam declares a method accepting
>>> java.lang.Integer (not int). The spec is very clear in requiring the
>>> signature of the lambda to be override-equivalent w.r.t. the signature of
>>> the Sam method.
>>>
>>> Maurizio
>>>        
>>>> Works (note argument is an int).
>>>>
>>>>    -- Howard Lovatt +61 419 971 263 (sent from my PDA)
>>>>
>>>> On 11/09/2010, at 10:22 PM, Maurizio
>>>> Cimadamore<maurizio.cimadamore at oracle.com>   wrote:
>>>>
>>>>
>>>>          
>>>>> The problem has nothing to do with try/catch - it is caused by using
>>>>> 'int' as explicit type - if you replace it with Integer it works...
>>>>>
>>>>> Thanks for the report.
>>>>> Maurizio
>>>>>
>>>>> On 11/09/10 06:18, Howard Lovatt wrote:
>>>>>
>>>>>            
>>>>>> Hi,
>>>>>>
>>>>>> Using my usual source:
>>>>>>
>>>>>>      143: final IntList11 il = new IntList11();
>>>>>>      144: out.println( il );
>>>>>>      145: il.forEach( #( int i ) { 2 * i } ); // No exception; no try
>>>>>> block - needs type parameter
>>>>>>      146: out.println( il );
>>>>>>
>>>>>> Gives:
>>>>>>
>>>>>> Exception in thread "main" java.lang.VerifyError: Bad local variable
>>>>>> type in method
>>>>>> lambdas.Main$9.call(Ljava/lang/Integer;)Ljava/lang/Integer;
>>>>>> at offset 1
>>>>>>     at lambdas.Main.exceptionTransparency(Main.java:145)
>>>>>>     at lambdas.Main.main(Main.java:24)
>>>>>>
>>>>>> But:
>>>>>>
>>>>>>      try {
>>>>>>        il.forEach( #( int i ) {
>>>>>>          if ( i<    4 ) { throw new Exception(); }
>>>>>>          return i * i;
>>>>>>        } ); // Need to catch the checked exception!
>>>>>>      } catch ( final Throwable notUsed ) {
>>>>>>        out.println( "Exception caught 1" );
>>>>>>      }
>>>>>>      out.println( il );
>>>>>>
>>>>>> Works!
>>>>>>
>>>>>> And:
>>>>>>
>>>>>>    il.forEach( #( i ) { 2 * i } );
>>>>>>
>>>>>> Requires a try catch block (as before) and:
>>>>>>
>>>>>>    il.forEach( #( Integer i ) { 2 * i } );
>>>>>>
>>>>>> Works.
>>>>>>
>>>>>> Great to see such regular updates into the repository,
>>>>>>
>>>>>>    -- Howard.
>>>>>>
>>>>>>
>>>>>>
>>>>>>              
>>>>>
>>>>>            
>>>
>>>        
>>
>>
>>
>>      



More information about the lambda-dev mailing list