2013-05-16 178 views
-3

下面是我在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() 
+3

在你知道什么样的方式,它的“错误”?它不是做你想做的事吗?你是否收到一条错误消息只是试图编译它? –

+0

@DavidGelhar是的,它确实会导致错误消息。 – AugustusCeasar12

回答

0

它有很多错误,所以很多它几乎不可能修改。但是,这些修复可能会帮助你,我试图根据你以前的东西来修复你的功能。我建议您在尝试使用metatables创建类之前先学习更多关于该语言的知识。

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 
+0

感谢您的意见,我对语言非常陌生,并且一般编程。很高兴知道有一个地方我可以要求提出富有成效的批评和基本解决方案。 – AugustusCeasar12

+0

@Augustus Ceasar很高兴知道它的帮助。这需要一段时间才能完成。不要犹豫,在SO上发布任何问题。 :) – HennyH

+0

'__newindex'有3个输入参数(t,k,v),'__newindex'的返回值没有任何意义。 –

相关问题