lua-users home
lua-l archive

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


On Mon, May 24, 2010 at 19:47, steve donovan <steve.j.donovan@gmail.com> wrote:
> On Mon, May 24, 2010 at 3:29 PM, Juri Munkki <jm_list@cameraid.com> wrote:
>> Ipairs allows you to add items to the list. If you evaluate the length of
>> the list in a numeric loop, you can't add new items to the list within the
>> loop and expect them to be looped through. Similarly, deleting items from
>> a table within the loop will behave differently with ipairs and a numeric
>> loop using #table.

> Very good point; the obvious & naive implementations of ipairs lack
> this very important property.

Funny thing that I was sure that it is undefined behavior, just like
with next/pairs.

        "The behavior of next is undefined if, during the traversal,
you assign any value to a non-existent field in the table. You may
however modify existing fields. In particular, you may clear existing
fields."

-- http://www.lua.org/manual/5.1/manual.html#pdf-next

When I write code with ipairs, I "think with the same part of the
brain" as when I use pairs. So it pays to have the same the same
mental blocks for both.

Alexander.