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

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

Michael Vanier wrote:

>>Date: Thu, 14 Aug 2003 10:45:34 -0400
>>From: "David B. Tucker" <dbtucker@cs.brown.edu>
>>>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?
> <cynic> 
> Otherwise you'd have the equivalent of true closures, and if you had that
> java would be a *really* powerful and useful language, so they obviously
> couldn't do that. 
> </cynic>
> This is my #1 pet peeve about java.  It drives me crazy.  So many styles of
> programming are made so much harder because of this.  But with real
> closures, the environment model of java would get more complicated, and
> presumably there would be efficiency costs as well, and I assume that the
> language's designers didn't want to deal with any of that.

But, they _do_ have the equivalent of true closures...  You just need to do
the mechanical translation:

X foo; => final X[] foo = new X[1];

and refer to foo everywhere as foo[0].

It sucks, but it works.  I always assumed they did this so that a lot
of the inner class stuff could be implemented as source -> source
transformations, but I guess GLS could probably give us a better