lua-users home
lua-l archive

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



On 11.04.2012 13:04, Dirk Laurie wrote:
_G.fact = fact
In Lua 5.2 make that _ENV.fact = fact.  It avoids the silly _G
convention *and* saves yet another table lookup.  :-)

Yes, _G should not be used when _ENV==_G.

But no, _G is not a silly convention.

1. It is a good idea to keep a reference to the original environment
     somewhere.
2. It is a good idea for everybody to use the same name for that
     reference.
3. It is a good idea to allow assignments to that name, so that
     Lua programs that wish provide a different default global
     environment (e.g. to disable some libraries) can do so.
4. _G is as good a choice for that name as any other.

Of course, you must bear in mind that _G is just _ENV._G.

I also avoid using the global _G. "1." is the only real use of it in specific situations, in other cases it just duplicates getfenv(0), but can't be relied on. So, add this at the beginning of what I wrote:

local _G = _ENV or getfenv(0)

I do think that this is better than using _ENV directly (with "local _ENV = _ENV or getfenv(0)" for 5.1 support). I can't think of cases where using _ENV directly may bite, but _G still feels better and it's 2 letters instead of 4 :-)

--
Best regards,
Sergey Rozhenko                 mailto:sergroj@mail.ru