[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Redefining locals
- From: "Dr. Rich Artym" <rartym@...>
- Date: Thu, 11 Nov 2004 15:57:25 +0000
Uh oh .....
On Thu, Nov 11, 2004 at 02:46:05PM +0000, Dr. Rich Artym wrote:
> Nope, that wasn't an example of it. The function modifies the "foo"
> inside its own closure which is an entirely different scope, not the
> "foo" in the old scope that was obsoleted by the second "local foo".
NOT SO!!! Bad assumption on my part. In fact, Lua 5.0.2 isn't closing
the scope at all. Instead of closure, it's implementing dynamic scoping!
So, as things stand, the overridden scope *is* still accessible.
Here's a slightly modified form of your example script. I've made the
function add only 0.1 increments to whatever it considers to be "foo",
and the initial foo is 10 (incremented to 11 within its first scope),
and overridden foo is 20, so it's more clear exactly what is happening:
-----------------------------------------------------------------------
local foo = 10
print(foo) -- prints 10
function incrementfirstfoo()
foo = foo + 0.1 -- On closure, non-local foo becomes local to closure
print(foo) -- If no closure, foo will remain external to function
end -- incrementfirstfoo
foo = foo + 1 -- This will not affect anything inside a closure
print(foo) -- prints 11
local foo = 20
print(foo) -- prints 20
incrementfirstfoo() -- prints 11.1 instead of 10.1 if it were closed
print(foo) -- prints 20
incrementfirstfoo() -- prints 11.2 instead of 10.2 if it were closed
print(foo) -- prints 20
incrementfirstfoo() -- prints 11.3 instead of 10.3 if it were closed
print(foo) -- prints 20
incrementfirstfoo() -- prints 11.4 instead of 10.4 if it were closed
print(foo) -- prints 20
-----------------------------------------------------------------------
How interesting. At least in 5.0.2, there are no lexical closures, all
I'm seeing is dynamic scoping without closure at all, at least in this
example. More investigation required I think.
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