lua-users home
lua-l archive

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


I see this was discussed back in January, when Lua 5 wasn't available. The
implementation of require I used to use with Lua 4 allowed cyclic
dependencies by simply marking a package as loaded when it started to
load. Is there a reason why this wasn't done in Lua 5's require? It would
basically just require a few lines of code to be moved in the
implementation.

The reason I ask is that when I first started converting my libraries to
Lua 5, I didn't have any loops (actually, I broke them as part of the
conversion). But now I do have cyclic dependencies, they're quite natural,
and factoring them out introduces awkwardness in the module structure of
the library.

For now, I'm just relying on the fact that most modules define a table, so
I can say, C-like,

if module then return end
module = {}

near the top of a library that is involved in a cycle.

But this is annoying and unnatural. Most of the time in Lua there's no
problem with cyclic dependencies, since forward references are effectively
unchecked, so you can load code in any order you like.

What I didn't ever see was a good reason given for making cyclic requires
generate an error. What sort of errors is this supposed to protect
against?

I wonder if this is worth reconsidering for Lua 5.1... (After all, making
require allow cyclic use wouldn't break any existing code, at least, not
sensible existing code!)

-- 
http://www.mupsych.org/~rrt/ | irrevocable, a.  expensive; see also lawyer