【发布时间】:2015-03-09 22:32:59
【问题描述】:
所以这是我写的一些代码:
local fileFunc = {createFolder, openObj, deleteObj, createTxt, theMenu}
setmetatable(fileFunc, mt)
function fileSys()
local fileAction, code
print("The File System")
print("You are currently at "..current_window)
while true do
print("1 Create a new Folder\n2 Open an object\n3 Delete an Object\n4 Create a new text file\n5 Other options")
fileAction = userInInt()
code = fileFunc[fileAction]()
if code > 3 then invRet("fileSys()", code) end
if code == 1 then return 0
else return code end
end
end
我以为通过使用__index 元方法,不会有错误,但它仍然会抛出attempt to call field ? 错误。我猜它仍然会抛出错误,所以有没有办法可以使用pcall()
mt 看起来像这样:
local mt = { __index = invalid }
而且无效:
function invalid()
print("Invalid operand, please try again.")
end
此错误仅在用户输入未在表中列出的操作数时引发 (input > #fileFunc)
【问题讨论】: