lua-users home
lua-l archive

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


Petite Abeille <petite.abeille <at> gmail.com> writes:
> To properly unpack a, hmmm, packed table, you need to explicitly specify it's 
> length:
> 
> local varargs = table.pack( nil, nil, nil )
> 
> print( #varargs )
> print( varargs.n )
> print( table.unpack( varargs, 1, varargs.n ) )
> 
> > 0
> > 3
> > nil	nil	nil

print(table.unpack({}, 1, 3))
nil	nil	nil

varargs.n is equal to the number of parameters passed to table.pack, it has
nothing to do with the content of the varargs table, except perhaps to state the
range of indexes the table.pack function attempted to store values in.

Section 2.1 of the 5.2 manual states:

"Tables can be heterogeneous; that is, they can contain values of all types
(except nil). Any key with value nil is not considered part of the table.
Conversely, any key that is not part of a table has an associated value nil."

Because the length operator is not stable, it does not make sense to me that
this would be the default in table.unpack.  Rather, it seems more interesting to
me to have table.unpack look for an 'n' index and use it.  Or make the index
range to unpack required parameters.  A default argument should be stable.

chris