lua-users home
lua-l archive

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



Roberto escribió:

> If the idea is for a package to receive the table where it should
> register itself, why not use the environment itself to be that table?
> So, the library simply defines its stuff "globally", and it goes
> automatically to the desired table? Something like this:

With a lot of respect, I like the basis of this idea, but I don't see
how it is pratical. All of the baselib functions used by the module
would need to be in the environment it was using, one way or another.
I actually experimented with this, using a metamethod to access base
library functions, but it wasn't really very pretty, and also leads
to oddities when baselib functions are being redefined, for example
in a sandbox.

The only real solution I could think of was to put all the baselib
functions in a table, too, but then a module writer needs to remember
to put something like:

   local base = require "base"

at the beginning of every module, and then you end up with a lot
of "base."s in your code. On the whole, I think adding one line to
the beginning and two lines to the end is not so difficult:

return function(module)
  -- ...
  return module
end

One thing that did occur to me was that this could be integrated
into the language:

-- module myStringLib

-- initialisation stuff, "use" calls, whatever

  module myStringLibrary
    method concat(a, b) ... end
    method split(s, pat) ... end
  end

which is syntatic sugar for:

  local function _fn(myStringLibrary)
    local function concat(a, b) ... end; myStringLib.concat = concat
    local function split(s, pat) ... end; myStringLib.split = split
    return myStringLibrary
  end
  -- the intention of these statements is to allow test code to
  -- go after the module definition
  if _REQUIRE then return _fn end
  local myStringLibrary = _fn{}


The program transformation is not so difficult but there are some
semantic issues having to do with the order of declaration of the
methods. Also, the fact that the module block would need to be
at the end of the file, since it inserts a return

However, I suspect that this is too radical. :)

Rici