lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 16 September 2014 18:25, Paul Bernier <bernier.pja@gmail.com> wrote:
I tried this snippet Dirk, but unfortunately it has the same pitfall.

Try this, it seems to work for me.

local proxy = {}

local mt = {
    __index = function(self, key)
        return proxy[self][key]
    end,
    __newindex = function(self, key, value)
        if not proxy[self] then
            proxy[self] = {}
        end
        proxy[self][key] = value
    end,
}

debug.setmetatable(function()end, mt)

local funcs = {}
for i=0,5 do
    local f = function() local _ = i end -- The dummy assignment should give each closure a unique upvalue i (suggested by LHF earlier in the thread)
    f.att = i
    table.insert(funcs, f)
end

print(funcs[1].att) -- I'd expect 0
print(funcs[2].att) -- I'd expect 1