lua-users home
lua-l archive

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


Björn De Meyer wrote:
I guess if you're willing to change your syntax a little, it'll
even be possible to do this all in lua itself yet again.
Something like:

namespace mynamespace

function foo() ... end

endnamespace

Where "namespace" and "endnamespace" are functions that perform the swapping and storing of the functions and the global environment... I'll look into
this next week at my work if I have the time.

That wouldn't be possible without changing the core. For one thing, the only time ()'s can be omitted from a function call is if there is exactly one argument that is a string or table. Also, since only a function scope may have its globals table manipulated, a wrapping function must exist to create the namespace.

The best that could be done without changing the core is:

  function namespace(mod_init)
    local G = getglobals()
    local mod_table = { }
    setmetatable(mod_table,{ __index = G })
    setglobals(mod_init, mod_table)
    mod_init()
    return mod_table
  end

and then

  local mod_table = namespace(function()
    local val
    function foo(x) ... end
      .
      .
      .
  end)

...which isn't that bad, but a little obscure for the newcomer.

-John



--
http:// i      .   /