lua-users home
lua-l archive

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

Simon Wittber wrote:

I have a few questions :-)

What is the purpose of using the local keyword in the
global scope?

There is no such thing, but I assume you mean a local at the
"top level" of a chunk, outside any function, like foo and
the second baz in the following example.  (A file is an
example of a chunk.)

-- Beginning of file.
local foo

function fun()
 local bar = baz -- This use of baz is a global.

local baz
-- End of file.

Here are some properties of this type of local that
distinguish it from a global:

- It is not visible outside its chunk.
- It is only visible below its declaration.  (This is why
 fun doesn't see the local baz in the above example.)
- It is a bit faster than a global (because locals are
 resolved at compile time and globals at runtime).