Is there a way to have
- Two Lua modules (let's call them
A
andB
) - Each module uses functions from the other, so they must
require
each other - A third module (let's call it
C
) can useA
but notB
e.g.
C.lua
:
local A = require 'A'
-- ...
A.foo()
- There may be another module
D
that requiresB
but notA
and/orE
requiring bothA
andB
- Neither
A
norB
nor their members should be added to the global namespace. - Avoid using the
module
andsetfenv
functions (deprecated in Lua 5.2)
Related: Lua - how do I use one lib from another? (note: this solution does not handle circular dependencies.)
require
from within a function like this? I've only ever seen it used at the top level. – Municipalize