lua-users home
lua-l archive

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


On Mon, May 2, 2011 at 11:21 AM, Xpol Wan <xpolife@gmail.com> wrote:
> As a beginner of Lua, I do think  select("#", ...) is not elegant enough.

I would agree that it is an odd-looking function. Better if it was
another function:

function count_args(...)
  return select('#',...)
end

That would at least give the operation a sensible name.

But ... is not a very elegant concept - we would probably all agree
that it is better than the old solution, which was a local implicit
variable called 'arg' containing the variable arguments as a table.

It's interesting to see how other languages handle the issue. If Lua
worked like Java, then vararg functions would be declared like so:

function V(args...)
 -- args is now a table of arguments (args.n will be set)
  ...
end

People want to avoid _having_ to pack a table, however. The Lua
solution allows our definition of count_args() above to be very
efficient.

steve d.