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

Re: bindings and assignments



Quoting Matt Hellige <matt@immute.net>:
> ["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.

I use it regularly in Java.

> > Out of curiosity, does anyone know why Java only allows final
> > variables to be referenced from within anonymous classes?
[...]
> This may be getting off-topic, but hey, you asked for it! Anyway, in
> my opinion this is one of the largest warts on Java... 

As everyone seems to be bashing Java for making this restriction, I'd like
to point out that languages such as Ocaml (ML style languages) have
similar (not exactly the same) limitation. In Ocaml, all bindings are
immutable, but arrays and record fields, for example, are/can be declared
mutable, which makes Ocaml an imperative language. It doesn't really
surprise me that people familiar with Ocaml aren't bitching about this
limitation.

One may argue that Ocaml aims to be (more) functional (than Java), but I
think that it is red herring, because if one thinks that the feature would
be useful in Java (making some imperative programs simpler), it should
basically be useful in Ocaml for the same reasons.

As far as I understand it, designing/implementing automatic boxing to
allow assignment to captured bindings isn't very involved (maybe a days
worth of design to implement in a well designed compiler) and allowing it
in a language (designed to be compiled for high performance) would mean that
only programs that use the feature (perform assignments to bindings in a
closure) would pay for the extra boxing, because the boxing needs to be
performed only when an assignment to a lexically bound variable are found.

Regards,
  Vesa Karvonen