lua-users home
lua-l archive

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

Rich Artym wrote:

> 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.

If Lua had dynamic scoping, the following program would
print "dynamic", but instead it prints "lexical":

    local foo = "lexical"
    function printfoo()
    end -- printfoo

    local foo = "dynamic"
    printfoo() -- Prints the value of of the variable
      -- referred to by foo when the function was closed,
      -- not the foo that's here where the function's being
      -- called.  Whether that foo can be changed or is just
      -- a snapshot or read-only is a different issue.

Incidentally, a colleague of mine just pointed out that Lua
4 had just the type of "snapshot" lexical scoping that
you're talking about.

(By the way, someone correct me if I'm wrong about Scheme
and Lua having exactly the same type of lexical scoping --
I'm not fluent in Scheme.)