【问题标题】:Can you please tell me what's wrong with this code? [closed]你能告诉我这段代码有什么问题吗? [关闭]
【发布时间】:2013-05-10 19:10:54
【问题描述】:

这是我刚刚在 SCIte 中编写的一些 Lua 代码,我不知道它到底有什么问题,所以有人可以向我解释我做错了什么以及如何解决它吗?

t = setmetatable({},{
__newindex = function(t, key)
if key == false then
  return( "False cannot exist in table")
  key = nil
  end
if key == __string then
  table.concat[table, key]
else
  table[key] = nil
  end
if key == nil then
  return  "Tables in this file cannot contain false values."
end
}
)

function Error()
  _,cError = pcall(__index)
end
function Call1()
  error("error in metatable function, '__index'", 1)
end
function Call2()
  Call1()
end

Error()

Call2()

【问题讨论】:

  • 你怎么知道它是“错误的”?它不做你想做的事吗?您是否只是在尝试编译时收到错误消息?
  • @DavidGelhar 是的,它确实会导致错误消息。

标签: lua syntax-error scite


【解决方案1】:

它有很多错误,很多几乎不可能修改。但是,这些修复程序可能会对您有所帮助,我已尝试根据您之前的内容在此处修复您的功能。我建议您在尝试使用元表创建类之前详细了解该语言。

t = setmetatable({},{
    __newindex = function(t, key)
        if key == false then
          return( "False cannot exist in table") -- return values in this function don't make sense
          --key = nil THIS WILL NEVER BE REACHER
        end
        if type(key) == "string" then --check key is a string
          table[key] = {}
        else
          table[key] = nil 
        end
        if key == nil then
          return  "Tables in this file cannot contain false values."
        end
    end
    }
)

进一步

function Call1()
  error("error in metatable function, '__index'", 1)
end

无厘头,总会输出错误,即:

error("No error here")

会产生

lua: ex.lua:26: No error here

【讨论】:

  • 感谢您的意见,我对这门语言和一般编程都很陌生。很高兴知道有一个地方我可以要求富有成效的批评和基本解决方案。
  • @AugustusCeasar 很高兴知道它有帮助。收拾东西需要一段时间。不要犹豫,在 SO 上发布任何问题。 :)
  • __newindex有3个输入参数(t,k,v),__newindex的返回值没有任何意义。
  • @EgorSkriptunoff 我认为他/她不关心第三个参数。我同意 __newindex 的返回值是荒谬的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 2021-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-21
相关资源
最近更新 更多