2013-05-16 176 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'的返回值沒有任何意義。 –

相關問題