[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Global by default
- From: joao lobato <btnfdp.lobato@...>
- Date: Tue, 12 Feb 2013 20:19:46 +0100
On 12/02/2013, Dirk Laurie <firstname.lastname@example.org> wrote:
> 2013/2/12 Peter Slížik <email@example.com>:
>> 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]
>> 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
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
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...