1
有没有办法在try/catch块中包装赋值表达式。包装异常代码
Window = {}
Window.mt = {}
Window.mt.__newindex =function(t,k,v)
if k=="x" or k=="y" then error("Readonly field", 2) end
t[k]=v
end
--w is a window 'type'
--try/catch this assignment
w.x = 50
感谢 EM
https://www.lua.org/manual/5.3/manual.html#2.3也https://www.lua.org/manual/ 5.3/manual.html#2.4您发布的代码不会像这样运行。 w是零,因此不能被索引,并且你没有在任何地方设置Window的metatable。 – Piglet