问题
尝试执行循环要求时,如何避免来自 Lua 5.1 的以下错误?
$ 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]: ?
文件结构
主程序.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
酒吧.lua
require 'foo'
Bar = {}
Bar.name = 'bar'
function Bar:getName()
return Bar.name .. Foo.name
end
预期产出
$ lua main.lua
foobar
barfoo