-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()
4

1 に答える 1

0

それには多くの間違いがあり、修正することはほとんど不可能です。ただし、これらの修正が役立つ場合があります。以前の機能に基づいて、ここで機能を修正しようとしました。メタテーブルを使用してクラスを作成する前に、言語についてもっと学ぶことをお勧めします。

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
于 2013-05-16T03:30:26.637 に答える