[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: require() passing arguments to module?
- From: Roberto Ierusalimschy <roberto@...>
- Date: Sat, 5 Nov 2011 11:14:18 -0200
> On Fri, Nov 4, 2011 at 10:10 PM, HyperHacker <hyperhacker@gmail.com> 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.
+1 for this answer :)
> Sometimes I do things like `local X = require 'foo' (...)`, which
> achieves the desired effect.
Or, to follow the usual protocol of returning a plain table:
local X = require 'foo' .init(...)
(Now the module has the option of allowing its use without parameters.)
-- Roberto