Discussion on table.remove

22 views
Skip to first unread message

Dirk Laurie

unread,
Jan 14, 2011, 7:43:57 AM1/14/11
to lua-table...@googlegroups.com
I see people are ranting about table.remove. I'll wait for
Roberto to slam them before inviting them in here!

Dirk

Axel Kittenberger

unread,
Jan 14, 2011, 7:53:43 AM1/14/11
to lua-table...@googlegroups.com
:) Well but in that case it really is a newbie error, the same thing
happens in C, removing an item from an array by memmove() is a bad
idea when an iterator is just running through it.
This is obvious in a close loop. But can be a real trap if its
distributed among code.

I had this very bug in Lsyncd myself where it was quite quite hidden
behind the scenes. In shortened pseudocode
FOR 1 TO number of listeners for filedescriptors DO
call the handling function for a filedescriptor number i.

One of the fd handlers had this:
IF end of pipe THEN
remove meself from list of filedescriptors....

Hard to spot when browsing the code, but obviously made it kaboom. I
had to do a flag "main code is just iterating through the fds" and in
the remove fds had to push it on the stack "wants to be removed"
instead of removing it right away, and after the iterator finished to
work through the stack of fds wanting to be gone.

So I take back my previous statement that # is currently the only big
trap for newbies. Modifing tables while iterating is another, or
assigning anything to a loop variable. The only way I see to make Lua
more bulletproof would be a readonly flag.

Henning Diedrich

unread,
Jan 16, 2011, 12:45:05 AM1/16/11
to lua table semantics
On 14 Jan., 13:53, Axel Kittenberger <axk...@gmail.com> wrote:
> :) Well but in that case it really is a newbie error, the same thing
> happens in C, removing an item from an array by memmove() is a bad
> idea when an iterator is just running through it.

I would say that anything that makes Lua hard for newbies should be
addressed?

C is not a valid argument here at all to my mind, because Lua excels
at not being C usually.

I understood that Lua is really taken to task by many on the list. And
though nobody actually spoke against it, I think newbies and non-
programmers are getting surprisingly short shrift. My interest is
probably explicitly to make Lua safe non-programmer newbies.

> So I take back my previous statement that # is currently the only big
> trap for newbies. Modifing tables while iterating is another, or
> assigning anything to a loop variable.

Interesting how it starts to be a list of issues ... three to my count
now, because I find the spec of insert(t,n,v) inconsistent as is,
doing overwrites at times.

Henning
Reply all
Reply to author
Forward
0 new messages