lua-users home
lua-l archive

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


On 30/06/2011 5.10, Daurnimator wrote:
On 30 June 2011 01:29, Lorenzo Donati<lorenzodonatibz@interfree.it>  wrote:

Mmm, but what's the use of pack, then?


local t,t_n = table.pack(...)

you can write also in 5.1.4:

local t,t_n = {...}, select('#',...)

and it isn't much more verbose.

table.pack is useful as it is because it is very short in the common case of
storing away all the vararg info in one shot (with n, so it handles possible
nils).

Perhaps, table.pack could still return n as a second argument, but the
injection of n in the table is very useful IMO.

print(unpack(t,1,t_n))

Daurn.




cheers
-- Lorenzo



The purpose of bringing back  table.pack is to make it so that doing 2
vararg related operations doesn't have to happen; apparently they are
quiet expensive: so table.pack is introduced where it figures out the
length and packs a table all at once.


Ok, so returning the length as a second argument is a good idea, indeed. But the injection of n in the table is good too. They are two different use cases. table.pack could support both.

Is it really a problem to have the table returned by table.pack contain n? If you really don't want it, it is easy to write:

t, n = table.pack(...)
t.n = nil

I think that it is also a bit more efficient than write:

t, n = table.pack(...)
t.n = n

this latter may trigger a rehash in the current implementation (but these are really implementation details).

-- Lorenzo