[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Thoughts on {...} and tbl[nil]
- From: Dirk Laurie <dirk.laurie@...>
- Date: Sat, 2 Jun 2018 08:06:38 +0200
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.