lua-users home
lua-l archive

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

>> Yes you can define a global variable inside a function. Variables without
>> 'local' are automatic global :-)
>Of course... I'm afraid you missed my point entirely. What I refer to is
>using local declarations in the global namespace, i.e. a program
>only consisting of "local x" (no functions, no nothing).

Then, I think you mean "top level in chunk", instead of "global namespace".

And yes, you can do that. In fact, having only local variables in a chunk
is a polite way of writing chunks -- loading such chunks does not overwrite
globals from others chunks by accident.

A more sophisticated use is to declare local variables in a chunk and use them
as upvalues in the functions that the chunk defines. This is a form of "static"
variables for chunks. Of course, the only real use is to define these locals
as tables, so that you can modify them later.