[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Table traverse
- From: Carlos Augusto Teixeira Mendes <cmendes@...>
- Date: Tue, 13 Oct 1998 12:01:49 -0300
Hi all,
I want to traverse a table and replace every function inside the table by a
stub function.
I came up with the simple code bellow (details of the stub function ommited):
i,v = next(tab,nil)
while (i) do
if type(v) == "function"
tab[i] = stub(v)
i,v = next(tab,i)
end
It works Ok, but Lua's manual description for the "next" function says:
"If the table is modified in any way during a traversal, the semantics of
next is undefined."
I can understand that including or removing a filed from a table could lead
to undefined behaviour, but why would the substitution of the VALUE of a
field (to another value different from nil) cause this? Isn't the
restriction imposed by the manual too tight? Acoording to it, I would
have to do something like:
newtab = {}
i,v = next(tab,nil)
while (i) do
if type(v) == "function"
newtab[i] = v
i,v = next(tab,i)
end
i,v = next(newtab,nil)
while (i) do
tab[i] = stub(v)
i,v = next(newtab,i)
end
newtab = nil
Another point is that the description of the "foreach" function doesn't say
a word about what happens if the table is modified by the function.
So, what should we do? Stick to the formal definition of "next" and use the
second solution or understand that the manual sentence is over restrictive
and use the first and much simpler solution (that works fine!!)? Any other
solution?
Thanks,
Carlos Augusto.