lua-users home
lua-l archive

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


At present a table literal can give you a haystack in which a needle
can't be found. I'm thinking mainly of {...} but to make things clearer
the example is an ordinary table literal.

    x = {nil,nil,nil,1,nil,nil}
    for k in ipairs(x) do print(k) end  -- nothing printed
    for k=1,#x do print(k) end  -- nothing printed

You are forced to use 'pairs' and check for the type of the index.

The now-shelved NIL_IN_TABLE would have solved this, but
we will have to wait for Lua 6.0.

A change that might be possible in a minor release is to make
`x[nil]` mean "the length of the table literal from which `x` was
constructed, if still known".

    for k=1,x[nil] do print(x) end  --> 1 2 3 4 5 6

It remains illegal to assign to x[nil].

The implementation would require one currently unused bit in the
table structure.