Accessing non-final local variables from a lambda expression

Rémi Forax forax at univ-mlv.fr
Fri Feb 26 00:44:07 PST 2010


Le 26/02/2010 09:23, Lawrence Kesteloot a écrit :
>> In BGGA, CfJ, and other languages with lambdas, one could write it
>> simply like this
>>
>> <T>  Iterable<T>  everyOther(Iterable<T>  input) {
>>     boolean flag = true;
>>     return filter(input, (T t)->(flag ^= true));
>> }
>>
>> But project lambda forbids access to mutable local variables from the
>> enclosing scope.
>>      
> <T>  Iterable<T>  everyOther(Iterable<T>  input) {
>     final boolean[] flag = new boolean[] { true };
>     return filter(input, (T t)->(flag[0] ^= true));
> }
>
> It's not beautiful, but not the end of the world either. And the
> performance is comparable, since in any case you have to create and
> dereference some heap object.
>    

+1

> Lawrence

Rémi


More information about the lambda-dev mailing list