只读lua中的可迭代表吗?

Hat*_*end 3 lua lua-table

我想在我的Lua程序中有一个只读表。如果密钥被删除或密钥与新值相关联,则必须引发错误。

function readonly(table)
    local meta = { } -- metatable for proxy
    local proxy = { } -- this table is always empty

    meta.__index = table -- refer to table for lookups
    meta.__newindex = function(t, key, value)
        error("You cannot make any changes to this table!")
    end

    setmetatable(proxy, meta)
    return proxy -- user will use proxy instead
end
Run Code Online (Sandbox Code Playgroud)

效果很好。

t = { }
t["Apple"] = "Red"
t[true] = "True!"
t[51] = 29

for k,v in pairs(t) do
    print(v)
end

t = readonly(t)
t[51] = 30
Run Code Online (Sandbox Code Playgroud)

版画

Red
True!
29
input:7: You cannot make any changes to this table!
Run Code Online (Sandbox Code Playgroud)

问题

for k, v in pairs(t) do
   print(v)
end
Run Code Online (Sandbox Code Playgroud)

现在在所有情况下都不会打印任何内容。这是因为proxy表永远不会有任何内容。pairs显然从不调用index,因此无法从实际表中检索任何内容。

如何使此只读表可迭代?

我正在使用Lua 5.1并可以使用以下元方法:

Lua 5.1手册

Ego*_*off 5

您可以修改标准Lua函数,pairs以使其与只读表一起正常工作。

local function readonly_newindex(t, key, value)
   error("You cannot make any changes to this table!")
end

function readonly(tbl)
   return
      setmetatable({}, {
         __index = tbl,
         __newindex = readonly_newindex
      })
end

local original_pairs = pairs

function pairs(tbl)
   if next(tbl) == nil then
      local mt = getmetatable(tbl)
      if mt and mt.__newindex == readonly_newindex then
         tbl = mt.__index
      end
   end
   return original_pairs(tbl)
end
Run Code Online (Sandbox Code Playgroud)

用法:

t = { }
t["Apple"] = "Red"
t[true] = "True!"
t[51] = 29

for k,v in pairs(t) do
   print(k, v)
end

t = readonly(t)

for k,v in pairs(t) do
   print(k, v)
end

t[51] = 30
Run Code Online (Sandbox Code Playgroud)