lua-users home
lua-l archive

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



I like the 'select()' approach, thumbs up!

It's better than the first(), second(), third() and so approach which I now use, cluttering the global namespace less and still doing the work.

-ak

19.8.2004 kello 23:47, Roberto Ierusalimschy kirjoitti:

 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