Problem
How can I avoid the following error from Lua 5.1 when attempting to do a circular require?
$ lua main.lua
lua: ./bar.lua:1: loop or previous error loading module 'foo'
stack traceback:
[C]: in function 'require'
./bar.lua:1: in main chunk
[C]: in function 'require'
./foo.lua:1: in main chunk
[C]: in function 'require'
main.lua:1: in main chunk
[C]: ?
File Structure
main.lua
require "foo"
require "bar"
print (Foo.getName())
print (Bar.getName())
foo.lua
require 'bar'
Foo = {}
Foo.name = 'foo'
function Foo:getName()
return Foo.name .. Bar.name
end
bar.lua
require 'foo'
Bar = {}
Bar.name = 'bar'
function Bar:getName()
return Bar.name .. Foo.name
end
Expected Output
$ lua main.lua
foobar
barfoo