lua-users home
lua-l archive

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


2012/12/12 steve donovan <steve.j.donovan@gmail.com>:
> On Wed, Dec 12, 2012 at 9:40 AM, Josh Simmons <simmons.44@gmail.com> wrote:
>> With 5.2 (and 5.1 too, really) pushing towards not registering globals
>> for libraries I'm not sure what the advantage of such a list would be
>> as it's going to be very heavily application dependent?
>
> It's better not to let random stuff leak into _G in the first place
>

Be careful not to turn the use of locals rather than globals into
a ritual that must be adhered to mindlessly.

I'm not advocating the leaking of random stuff into _G.  But globals
don't sit in _G, they sit in _ENV. It is perfectly possible, in fact
it is idiomatic in Lua 5.2, to use _ENV in an encapsulated way.

do
  local imports = imports -- from the containing _ENV
  _ENV={}
  -- globals defined from here on do not leak into _G
  -- etc
end

Globals avoid the need for forward declarations.  Used in the right
way, it's basically the same as putting everything into some
special-purpose table.

Anyway, the point of Thijs's question is not so much about global
versus local.  It is about conventions for certain names.  For example,
if I start up some script that preloads some stuff, and I see a Lua
prompt, would it not be nice if there is a good chance that typing `help()`
or `=help` does something helpful, and even better if we could agree
which?  I don't really care whether names like `lfs` and `lpeg` have
been declared local or global by the time I get in. But I would strongly
object if `lpeg` is defined but actually means "linear programming example
generator".

What we should be compiling is a sort of conventional glossary, with
a broad consensus that any name in that glossary should either be used
in an accepted sense or not used at all.

Or are Lua programmers just too individualistic for this ever to work?