lua-users home
lua-l archive

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


On Mon, Mar 31, 2014 at 9:37 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
> 2014-03-31 15:15 GMT+02:00 Thomas Jericke <tjericke@indel.ch>:
>
>> http://lua-users.org/lists/lua-l/2002-04/msg00177.html
>>
>> See, the Lua team was there already. For me it's a petty that they dropped
>> those ideas. I have myself a metatable on the globals to support this stuff.
>> But a general solution to such a basic problem would be nice.
>
> Some history:
>
> 1993 Lua released.
> 1995 Lua 2.1 provides fallbacks.
> 1997 Lua 3.0 replaces fallbacks by tag methods.
> 2003 Lua 5.0 replaces tag methods by metamethods.
> 2011 Lua 5.2 stores globals in _ENV.
>
> I would not say that the Lua team dropped the ideas. On the contrary,
> starting just the year after that post, they present a general solution so
> basic that user control over globals is merely one of the many things
> it solves.
>
> Honestly, I can't understand the squeamishness some people have
> about _ENV. You are allowed to treat it like any other table, including
> equipping it with metamethods, so do it. That is not dodgy programming,
> it is exploiting features the designers meant to make available to you.
>
> strict.lua is not supposed to be a tool everybody should use, it is
> a sample module demonstrating just one powerful thing that
> metatables allow you to do. This is how Teacher paints a cat,
> now you go and paint a leopard for me.
>
> And _G. There's nothing sacred about _G. It's merely another name
> for the table containing the lowest-level _ENV. The only claim made
> for _G in the Lua manual is that nothing in Lua and its libraries ever
> changes _G. That's not because _G is a holy cow, it is because
> _G is good for nothing besides stopping the first _ENV from being
> garbage-collected. _G is not being worshipped, it is being ignored.
>


Yes, and I love the simplicity and power of _ENV and I use it all of
the time. I also use a variant of strict, as well. Part of what
reminded me (again) of why Lua treats globals as it does is a short
exchange with Roberto in which I asked a similar question (I believe
regarding constants) and he replied with (I may have this wrong) "you
want fallbacks" --- the important part being that he was saying that
what I was looking for was a metamethod that would be triggered on
*every* table access.

The point being, and expressed here for my own reinforcement: It's
never okay to forget that globals are a table and that anything that
treats their values differently either breaks that simplicity or adds
something major to tables, generally.

--Andrew