[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bindings and assignments (was: Re: continuations)



At 10:45 AM -0400 8/14/03, David B. Tucker wrote:
>  > I imagine, though I don't have statistical evidence, that the
>>  requirement of declaring local variables to be final in order to
>>  reference them within anonymous inner classes (closures) is almost
>>  entirely unknown and unused in practice.
>
>Out of curiosity, does anyone know why Java only allows final variables
>to be referenced from within anonymous classes?

If you allow the captured variables to be side-effected,
it means that you have to work harder to ensure that the
the captured variable is properly shared between multiple
inner classes that share the lexical environment.  That
is, if you only allow final variables to be captured,
you can just copy the value into the "environment" object,
but if they can be written to, you have to introduce a
"cell".  To this outside observer, this is a classic
example of implementor laziness -- it would have been
"hard" to do the right thing, so they did the wrong thing,
and everyone who wants inner classes to be useful has
to introduce a bunch of Cell classes manually.