Discussion:
Recursive require
Reuben Thomas
2006-03-21 13:42:36 UTC
Permalink
I'm just updating my stdlib package for Lua 5.1, and I find that
although I was already using Kepler's compat-5.1.lua, there's a
difference with the real thing in the behaviour of require: namely, Lua
5.1 complains when require loops (a requires b requires c requires a).

I can see ways of getting round this, e.g. setting package.loaded at the
top of a module, but how should I be doing this? (Answers suggesting
"get rid of your loops" will not be looked on kindly: a straightforward
grouping of functions into modules by functionality results in loops,
e.g. bind (from std.base, to partially bind a function) calls
list.concat, while the std.list calls lots of functions in base.)

compat-5.1.lua sets package.loaded["name"] before loading the module, so
loops are fine. (I do see the use of unwanted loop detection.)
--
http://rrt.sc3d.org/ | maximiste, n. pessimiste (Roux)
Wim Couwenberg
2006-03-21 17:31:03 UTC
Permalink
Post by Reuben Thomas
Lua
5.1 complains when require loops (a requires b requires c requires a).
Change the order of require and module calls. Example:

--- file a.lua ---

local print, require = print, require
module "a"
require "b"
print "module a"

--- file b.lua ---

local print, require = print, require
module "b"
require "c"
print "module b"

--- file c.lua ---

local print, require = print, require
module "c"
require "a"
print "module c"

---------
Post by Reuben Thomas
require "a"
module c
module b
module a

--
Wim
Reuben Thomas
2006-03-21 17:33:29 UTC
Permalink
Post by Wim Couwenberg
Post by Reuben Thomas
Lua
5.1 complains when require loops (a requires b requires c requires a).
Change the order of require and module calls.
It seems my main problem was that I didn't even know about the module
function. Thanks.
--
http://rrt.sc3d.org/ | art, n. romanticized mundanity
Continue reading on narkive:
Loading...