lua-users home
lua-l archive

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


"John Belmonte" <jvb@prairienet.org> writes:

> I understand Lua programs much better than scoping lingo.  Lua
> upvalues cannot be said to be lexically scoped because the following
> program prints "5" instead of "10"?
> 
>     do
>         local a = 5
>         local foo = function() print(%a) end
>         a = 10
>         foo()
>     end

Correct.  Statements inside the function bound to foo in the do block
cannot access its local variable a.  If it could, a function bound to
foo could be written that accesses the value of a and then prints it,
in this example displaying "10".  In Lua, the current binding of a
local variable is hidden from any function defined within the scope of
the local variable.  This is a problem only if the binding is changed
between the time at which the closure for the function is created, and
the time at which the function is executed.

John