lua-users home
lua-l archive

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


On Sat, Dec 11, 2010 at 11:38:35AM +0200, Richard Hundt wrote:
> One thing I've noticed is that although one can create a sparse table 
> using literal syntax:
> 
> local t = { 1, nil, 3 }
> 
> there doesn't seem to be a programmatic way of achieving the same thing. 
> Would it be possible to add support for:
> 
> local t = { }
> table.insert(t, 1)
> table.insert(t, nil)
> table.insert(t, 3)
> 
Please correct me if I'm wrong, but this is my perception:
you as a programmer are not entitled to know whether t[2]
is stored as a nil or is simply missing, let alone rely on
such a distinction being in effect.  

Furthermore, if you say
    local t = { 1, nil, 3 }
the Lua 5.2 standalone interpreter reports #t as equal to 3,
but there is no reason to suppose that any other implementation,
such as luajit or even the next (beta?) release of Lua 5.2, will 
do so too.  

For all we know (I have not actually perused the source code) the 
internal data structure for t has some dating back to from Lua 5.0 
that contains a field t.n, only we can no longer see it, and uses
that field, if present, as the first possibility to try when
calculating #t. 

Dirk