我从教程中获取了此信息,当我尝试运行它时,出现了一个令人困惑的错误
local t1 = {"hi", true}
local t2 = {79, "bye", false}
local mt = {
__concat = function(v1, v2)
local output = {}
for i, v in pairs(v1) do
table.insert(output, v)
end
for i, v in pairs(v2) do
table.insert(output, v)
end
end
return output
}
setmetatable(t1, mt)
setmetatable(t2, mt) --they gotta have it both as well
local t3 = t1..t2 --we merged t1 and t2 together, as you can see you can get creative
print(unpack(t3)) --t3 contains all of t1 and t2's members
The error is : File:13: '}' expected (to close '{' at line 3) near 'return'
最佳答案
您的问题:错误地从字面上理解错误消息,却没有花时间去理解它。
Lua不仅在第13行随机地期望}
,还期望在表文字中有意义的东西并找到return
,因此默认情况下告诉您它可以想到的第一件事。首先用}
关闭表格。
真正的问题是return
在函数之外,因此Lua不知道该怎么做。它需要超过end
。
一般而言:当Lua告诉您X线上有期望时,您应该阅读它,因为我在X线上发现了一些奇怪的东西,却不知道您的意思。我猜在表文字中会遇到更好的错误消息return
。
关于lua - 已经放入},但错误仍然表明}是预期的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63032427/