[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: require() passing arguments to module?
- From: David Manura <dm.lua@...>
- Date: Fri, 4 Nov 2011 22:44:19 -0400
On Fri, Nov 4, 2011 at 10:10 PM, HyperHacker <firstname.lastname@example.org> wrote:
> I feel like this idea has been brought up before, but I don't recall
> the consensus. Presently, require() accepts only one argument. Why not
> change it to require(name, ...) and pass all additional args on to the
> module? The module can then retrieve them from ... just as it would if
> you did loadfile(name)(...).
Now sure about consensus, but in `loadfile(name)(...)` you are passing
the arguments to the loader. Once require loads a module it caches
it. So, if another call is later made to require for the same module
but with different arguments, the new arguments are ignored because
the loader is not re-invoked again. You therefore have in a way a
race condition where the behavior depends on the order in which
modules are loaded.
Sometimes I do things like `local X = require 'foo' (...)`, which
achieves the desired effect. IMHO, the 'strict' module in Lua 5.1
should have operated this way, with the environment to strict-icize
passed as the argument.