lua-users home
lua-l archive

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

On 24 Nov 2013, at 13:12, John Hind <> wrote:

> 2. 'ipairs' and its associated metamethod are redundant - just use the numeric form of 'for'.

Not sure I agree with this entirely. The greatest thing about ipairs for me is that it lets you just loop over the entries in order without worrying about the index. If you have a few nested loops, where you don't care about the index in any of them, just the array entry, then saying:

for _, o1 in ipairs(t) do

is a lot nicer, and less error prone than

for o1_i=1,#t do local o1 = t[o1_i]

In regards to the metamethod, I've never needed it personally, but isn't it there for cases where t[i] is a very expensive operation, but iteration can be done much cheaper. Say for example

1) Iterating over a linked list (__ipairs can cache the current node)
2) Where the data for t[i] is accessed over a network (__ipairs can fetch it in bulk because it knows it's iterating)