lua-users home
lua-l archive

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

On Fri, Nov 12, 2004 at 08:15:55AM -0800, Mark Hamburg wrote:

> It's the pairs that are captured as in they continue to refer to the same
> pairs. 

Correct, and these pairs are (name,value) tuples, not (name,var-address)
tuples, which would be entirely foreign to the concept of closures.
The (name,value) pairs are in the closure so that each occurrence of
"name" can be replaced by "value" at eval time in this type of interpreter.
It simply provides one method of closing the open functional expression.

> If that pair is visible elsewhere, however, then changes to that pair
> within the function will be visible outside the function and vice-versa.

In which case you have not a closure but an ordinary open function.  And
that is *exactly* what Lua has --- my point precisely.  A closure implies
that the "value" of (name,value) pairs is not modifiable, since if it is
modified then it clearly wasn't closed.  Languages that derive from LISP
tend to use the old environmental implementation of the captured pairs
simply because it's easier.  But that is not fundamental to the concept
of closures, quite the opposite, it's just a particular implementation.

There's lots of other implementations, like I mentioned, which create the
closure in ways that guarantee that the state is closed and immutable.  It's
the basis of all that is good with closures via referential transparency.

> No new environment is constructed when the function is created. Instead it
> retains a reference to the environment in which it was created.

That's just an implementation detail.  It doesn't happen in copy-based
interpreters for example.

If yyou haven't closed the states of the free variables of the function
then you can't claim to have created a closed function, it's that simple.
What you have is still a function with free variables, not a closure.

Rich Artym.
Existing media are so disconnected from reality that our policy debates
spin around a fantasy world in which the future looks far too much like
the past.