[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua 5.1+ and variable-argument tables
- From: Roberto Ierusalimschy <roberto@...>
- Date: Thu, 19 Aug 2004 17:47:45 -0300
> Does it make sense to add a `pack' function to the base lib that sets
> the `n' field (as the arg table did)?
We plan to add a function that returns its number of arguments, so
you can write
local arg = table.setn({...}, newfunction(...))
It is more verbose than a function that packs and sets n at once, but
there will be times where we do not want to pack.
We also plan a new function select(i, ...), that returns its i-th
argument. So you can loop over the varargs with something like
for i=1,newfunction(...) do
local a = select(i, ...)
dosomethingwith(a)
end
With all overheads of function calls and parameter passing, this loop
is much faster than packing the arguments, for few arguments (less than
four or five).
select is also useful for other purposes, like
local e = select(2, string.find(...))
-- Roberto