lua-users home
lua-l archive

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


On Thu, Nov 11, 2004 at 11:04:13AM -0500, Aaron Brown wrote:

> This is exactly how Lua lexical scoping is supposed to
> work -- just like Scheme lexical scoping (i.e., "true"
> lexical scoping).  I believe in some other languages
> upvalues work like you're thinking of (i.e., they get turned
> into local variables internal to the function).

Hehe, you have that exactly 180 degrees back to front about
lexical scoping. :-)  I don't doubt for a moment that that's
exactly how Lua scoping was meant to work, but it's not lexical
scoping at all, but very normal dynamic scoping.  LISP allows
both dynamic and lexical scoping, the choice is explicit.

Dynamic scoping is exactly what you are describing:  the function
is not a pure function but has external variables which affect the
semantics of the function.  It's dynamic, not lexical.  Closures
are not needed for dynamic scoping, in fact they prevent dynamic
scoping by design because they capture the bindings of what are
traditionally called "free" variables.  This applies to both global
variables and local variables outer to the scope of the function.

Incidentally, that's why closures are called closures, ie. because
they capture their environment at the moment the closure is formed.
If the environment is kept open, it's not closed.  The foo in your
function is still a free variable! :-)

I'll try to dig up some references.

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.   http://www.zyvex.com/nanotech/MITtecRvwSmlWrld/article.html