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

Re: Closures



> Jeff Dalton wrote:
> > I suspect the reason's the same as in Java (where you can do the Java
> > equiv of closing over variables only if they're "final") - there's an
> > easy implementation if you copy bindings instead of sharing them, and
> > you can hide the semantic difference by forbidding assignment.
> 
> This is only true in Java for local variables.  Fields in a surrounding
> scope are still shared and mutable.

That's why I used the term "variable".  :)

> It was never clear to my why this was done for Java.  It would not have
> been very hard for the compiler to rewrite the function to replace local
> variables with length-one arrays and then pass  those.  Whatever.

They did it in such a way that they could later generalise to
non-final variables if they wanted to.  Presumably they thought the
extra rewriting effort needed to make assignment work properly wasn't
worth it.

Or maybe they thought that the assignment semantics was too hard to
explain or that using variables in that way was somehow wrong.

It's a good question.

-- Jeff




Follow-Ups: