lua-users home
lua-l archive

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

2013/4/26 Steve Litt <>:

> I can't think of any reason for a global variable's
> existence to be conditional.

That's because you are accustomed to having no distinction between
nonexistent and nil-valued global variables. Highly convenient.

The main difference _in practice_ between a local and a global variable
is that `local x` (which is syntax sugar for `local x=nil`) fixes the
point on the main Lua stack where `x` will be stored. It uses up one
of the precious 200 slots in the current stack frame that are available
for the purpose. Whereas `x=nil`, if `x` is a global variable, does not
reserve any of the practically unlimited number of available slots in
the current _ENV upvalue.

Is anybody regularly using the following idiom for temporarily grabbing
200 slots for the price of one?

    (function ()
       -- code goes here

Or the following idiom for making the global namespace read-only?

    function well_behaved_routine()
       local _ENV=setmetatable({},{__index=_ENV})
       -- code goes here