Lua小技巧

来公司以后,业务逻辑都用lua写。写了好长时间了,到最近才觉得有点掌握了Lua的灵活。最近用Lua写了个类似集合一样的东西,如果两次向集合里放入同一个元素,就会报错,方便检查配置。代码如下:

-- keep data across rule
local m = {}
local where_defines = {}
local getinfo = debug.getinfo
name2id = setmetatable({}, {
        __index = m,
        __newindex = function(self, key, value)
            if not m[key] then
                m[key] = value
                where_defines[key] = getinfo(2).short_src
            else
                local new_define = getinfo(2).short_src
                local where_define = where_defines[key]
                print(where_define.." defines "..key.." : "..m[key])
                print(new_define.." defines "..key.." : "..value)
                assert(false, "redefinition of key:"..key)
            end
        end
    })
local M = {
    name2id = name2id,
}
return M

由于Lua的__index hook不完整,只有在数据不存在的时候,才会索引元表的__index,所以需要将数据藏起来。之前是用一个key为data,value是空表的子表去存实际数据,这样做会有一个隐患,当塞进去的key是data,就会把其他数据覆盖掉。后来同事提醒,可以用upvalue,先声明一个本地table m,然后在元表方法里引用之,这样外层就不会误改数据域了。要找回m,可以从元表中查回来。

原文地址:https://www.cnblogs.com/Lifehacker/p/lua_little_tricks.html