lua-users home
lua-l archive

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


>In Lua 4, sure.  But I thought that we were "lexically scoped" now.
> Doesn't that mean that a is automatically local to the function?
> Or have I misunderstood?
>Andy.

You've misunderstood.  You still need the local statement to make a local
variable.  Because of "lexical scoping" you have a lot more options on how
local a "local" is, but Lua still defaults to global unless you use the
local statement.  For instance...


Foo = 1
Bar = function() do
    local Foo = 5

    do
        local Foo = 7 -- I <think> this is illegal in Lua 4
        return Foo
    end
end

contains 3 separate Foo's, while ...

Foo = 1
Bar = function() do
    Foo = 5

    do
        Foo = 7
        return Foo
    end
end

contains only 1 global Foo.