lua-users home
lua-l archive

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


>Consider this somewhat common idiom:
>
>   -- print and remove all elements from a list 't':
>   while true do
>     local e = table.remove(t, 1)
>     if e == nil then break end
>     print(e)
>  end

>That code would break if 'table.remove' did not accept len+1.

>-- Roberto

That is a good point, Roberto. However it is worth noting that in Lua 5.2.1,
your example works because table.remove returns nothing when pos>t#, and
leaves t alone. It seems an unnecessarily radical change to move pos==#t+1
into the range that table.remove operates on. Can I suggest it would be
safer to return nothing for pos==#t+1, and error for pos>#t+1? (or a case
could be made for returning nothing for pos>#t, so only pos<1 would be an
error).

My concern is that # (or more accurately luaL_len) uses any '__len'
metamethod, so we really should not be depending on the assumption that
t[#t+1]==nil (or that t[#t+2]==nil for that matter).

- John