lua-users home
lua-l archive

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

On 12/02/2013, Dirk Laurie <> wrote:
> 2013/2/12 Peter Slížik <>:
>> Okay, here's the new thread I announced a while ago...
>> Lua-users wiki cites Roberto's words:
>> "Local by default is wrong. Maybe global by default is also wrong, [but]
>> the
>> solution is not local by default."
>> Could anybody who understands Lua innards elaborate?
> One possible reason might be that Lua only allows so many
> local variables, 200 I think, the number must fit into one byte
> otherwise the whole VM needs to be redesigned. Whereas
> the number of global variables is for all practical purposes
> unlimited.

IIRC, although unrelated to innards per se:
 - local by default is wrong because while it's uncommon that you want
to refer to global variables -- because globals are a Bad Thing (tm),
it is not uncommon to want to refer to non-local, non global
variables, i.e., upvalues:

a = 1 -- global
  local a = 2
    local a = 3
      function f()
        local a = 4

if you were to define that omitting the local keyword for a=4, if you
wanted to refer to a=2 and a=3 you would not be able to use a keywork
like 'upvalue' -- because you have more that one upvalue called 'a'
(sure, they shadow each other, but it would still be a gotcha).

 - global by default is bad because you want to avoid globals as much
as possible (for encapsulation, performance, etc. ). Moreover, _G['a']
is just a special case of _ENV['a'] in 5.2.

This discussion rekindles an old firestorm...