lua-users home
lua-l archive

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


2012/12/9 John Hind <john.hind@zen.co.uk>:

> Not sure I understand how the latter two suggestions differ, but the first
> certainly requires a constructor function in the global namespace for every
> class which is what I am trying to avoid. My way, the classes occupy their
> own private namespace and even the "class" method is not global existing
> only as a method of Table type.

In Lua 5.2, "the global namespace" is no longer something to be kept tidy
at all costs. You write

    do
       local A,B,C = A,B,C -- import things from the enclosing environment
       local _ENV={} -- global namespace is now empty
       -- code that can export only via the imported tables
    end

Moreover, one can (in fact, should) put such constructors into
packages.  container.list, container.set etc. The C++ STL shows
how it should be done.  Sooner or later you need to use something
global.  Better IMHO to do it explicitly and visibly rather than
sneaking it in via a subtle property of a newly constructed table.