Effectively final
Per Bothner
per at bothner.com
Fri Jul 29 11:33:08 PDT 2011
On 07/29/2011 04:31 AM, Tim Fox wrote:
> AIUI most languages which support closures/lambdas do allow local
> variables to be mutated from within the closure.
As mentioned before one problem with this is capturing loop control
variables,
because you could end up capturing the last value, rather than the value
of the corresponding iteration. It's worth noting this is not an issue
for the Scheme language (which basically introduced closures) because each
iteration is defined to create a *new* set of the loop control variables.
That is because the loop is syntactic sugar for a recursive function
definition
and call, and the "loop control variables" are function parameters: Each
iteration is a tail-call, with a new set of parameter instances.
(Conceptually:
Of course this is normally optimized to a loop, and the loop variables
reused.)
There is an ambiguity with mutable loop control variables: Are they local
to a specific iteration, or is there a single variable shared across all
iterations? Sometimes you want one; sometimes you want the other.
Unfortunately,
the traditional for-loop has IMO the default the wrong way, and we can't
fix it
(The Java 5 enhance for loop correctly makes the iteration variable local
to each iteration.)
Unfortunately this mess is because of one of the "features" that helped
make Java
successful when it first appeared: Using C syntax and control structures.
--
--Per Bothner
per at bothner.com http://per.bothner.com/
More information about the lambda-dev
mailing list