On Tue, Oct 5, 2010 at 3:10 PM, Matthew Wild
<mwild1@gmail.com> wrote:
module/require aren't deprecated in 5.1. In 5.1 you can avoid global
pollution by not calling module(), but returning a module table
instead. The loader would then have to do "local foo = require 'foo'"
to capture your return table.
You said no prefix, this can be done with setfenv, metatables, or
manually pulling functions into the current environment (an import()
helper function could do this).
Yes I already know some ways in 5.1 like the ones you mentioned. I was specifically interested in how this is supposed to be done in 5.2 with the deprecation of those functions.
In 5.2 you could do the same thing in other ways, considering _ENV.
-- mymodule.lua
return function (_ENV)
_ENV = _ENV or {};
function exportedfunc1(foo, bar)
end
local function privatefunc1(foo)
end
return _ENV;
end
-- requirer.lua
require "mymodule" (_ENV) -- Loads into the current environment
-- OR
local mymodule = require "mymodule" () -- Loads into empty environment
and returns
Obviously having to call a module is a departure from how we do things
today, but this seems the most natural approach to achieve all your
goals.
Hmm that doesn't excite me very much no.
It makes me wonder though if something like this couldn't be built
into 5.2's require() (a second parameter) to compensate for the loss
of module()...
It looks to me like something along those lines would be helpful. I still don't get why those functions are deprecated in 5.2 and the better alternative would be, but probably I'm just missing the bigger picture.
Thijs