lua-l archive
[
Date Prev
][
Date Next
][
Thread Prev
][
Thread Next
] [
Date Index
] [
Thread Index
]
Subject
:
Re: Lua 5.2 new function reference inside a loop
From
: Choonster TheMage <choonster.2010@
...
>
Date
: Tue, 16 Sep 2014 18:36:29 +1000
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
Follow-Ups
:
Re: Lua 5.2 new function reference inside a loop
,
Tom N Harris
References
:
Lua 5.2 new function reference inside a loop
,
Paul Bernier
Re: Lua 5.2 new function reference inside a loop
,
Andrew Starks
Re: Lua 5.2 new function reference inside a loop
,
Luiz Henrique de Figueiredo
Re: Lua 5.2 new function reference inside a loop
,
Andrew Starks
Re: Lua 5.2 new function reference inside a loop
,
Coda Highland
Re: Lua 5.2 new function reference inside a loop
,
Enrico Colombini
Re: Lua 5.2 new function reference inside a loop
,
Paul Bernier
Re: Lua 5.2 new function reference inside a loop
,
Dirk Laurie
Re: Lua 5.2 new function reference inside a loop
,
Paul Bernier
Prev by Date:
Re: Lua 5.2 new function reference inside a loop
Next by Date:
Re: How to approach this?
Previous by thread:
Re: Lua 5.2 new function reference inside a loop
Next by thread:
Re: Lua 5.2 new function reference inside a loop
Index(es):
Date
Thread