lua-users home
lua-l archive

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


On Mon, Jul 12, 2010 at 8:29 PM, Nevin Flanagan <Alestane@comcast.net> wrote:
> ...one thing that has seemed sensible to me and does not seem to be
> implemented is the options of giving extra arguments to require, and
> having them passed to the chunk loaded...that is, if you called
> require(name, ...), name.lua would be called with ... as arguments to the chunk.


`require(name)` loads `name`, executes its loader function, and caches
the result by key `name`.  Future invocations to `require(name)` will
return that cached result, regardless of additional arguments.
Therefore, this caching can be incompatible with `require` having
arguments.

All is not lost though.  If you need side-effects unique to each
import, you need to call some other function following `require` to
pass your arguments to.  For example,
`require(name):import(arguments...)`.  I've proposed that Lua come
with a built-in `import` function that wraps `require` and calls
`import` so that this can be written more simply as `import(name,
arguments...)`.  This more properly supports what are sometimes called
"pragma modules", which are modules imported to achieve some special
side-effect in the caller (sort of like "strict.lua"). [1,2]

[1] http://lua-users.org/lists/lua-l/2009-09/msg00438.html
[2] http://lua-users.org/lists/lua-l/2010-01/msg00456.html