Method References

Rémi Forax forax at univ-mlv.fr
Fri Feb 26 02:45:06 PST 2010


Le 26/02/2010 10:01, Fredrik Öhrström a écrit :
> Neal Gafter skrev:
>    
>> Method References (ala CfJ 0.6a) would be a valuable addition to Project
>> Lambda's specification.  While the additional specification text is fairly
>> small, it enable a number of useful idioms that would otherwise be quite
>> verbose.
>>
>> The most obvious use case will be lambdas that would otherwise have to be
>> written this way
>>
>> #(Type1 arg1, Type1 arg2, ...)(EnclosingClass.this.method(arg1, arg2, ...))
>>
>> because they could be written like this
>>
>> this#method(Type1, Type2, ...)
>>
>> This simplifies a common pattern seen in code today that registers
>> callbacks
>>      
> Yes, this is a very important and common use case. If the parentheses
> and types are unnecessary, for example there is only one single method
> with that particular name, please allow the reference to be created
> without the types. Ie:
>
> cb = this#saveState;
>
> will work as expected as long as there is only a single saveState in this.
>
> //Fredrik
>    

Hi Fredrik,
there is a source compatibility issue if you allow to not specify the type.

class A {
   void m(String s) { ... }
}
....
ref = A#m;

Now suppose I want to add a new method m(Integer),
because String and Integer an unrelated, currently there is no source 
compatibility issue.

If reference can be created without types,
this change is no more compatible because the compiler should raise an 
error.

Rémi




More information about the lambda-dev mailing list