在lua中只读可迭代表?

时间:2017-12-23 23:43:15

标签: 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

效果很好。

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

打印

Red
True!
29
input:7: You cannot make any changes to this table!

问题

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

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

我可以做些什么来使这个只读表可迭代?

我正在使用Lua 5.1并可以访问这些元方法:

Lua 5.1 Manual

2 个答案:

答案 0 :(得分:4)

您可以修改标准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

用法:

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

答案 1 :(得分:1)

一种解决方案是为表创建一个完全自定义的迭代器。

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

    local function iter()
        return next, table
    end

    setmetatable(proxy, meta)
    return proxy, iter -- user will use proxy instead
end

用法:

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

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

t, tIter = readonly(t)
t[51] = 30

for k, v in tIter do
   print(v)
end