Accessing non-final local variables from a lambda expression

Paulo Levi i30817 at gmail.com
Fri Feb 26 18:30:31 PST 2010


I ask again, why not move the , into the method that uses the lambda:

<T> Iterable<T> filter(
   Iterable<T> input,
   Predicate<T> predicate)
{ ... }
ok:
int a = 0;
filter(list, (int i){
a+= i;
return true;
});

<T> Iterable<T> parallelFilter(
   Iterable<T> input,
   lambdaFinal Predicate<T> predicate)
{ ... }
syntax error:
int a = 0;
filter(list, (int i){
a+= i;
return true;
});

If the trype is in the variable being used that is the decision of the user,
not of the library designer. The user must know (remember) that the function
he calls is parallel and that shared state is non-deterministic.


More information about the lambda-dev mailing list