lua-users home
lua-l archive

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


Hi,

there's been a notice that the next Lua version gets new varargs.

I assume that the args table will be gone and the dots may then be
used within the body of the function to produce the varargs
(behave like a function call that returns all variable arguments).

I observed one point while using these varargs in Sol: I often
wished that I were able to assign to "...".  I don't know whether
this will be possible in Lua but it would be handy.  I.e.

  local start, end, ... = string.find(str, somepattern)
  print("captures", ...)

I always had to escape to a helper function for stuff like that:

  function helper(start, end, ...)
    print("captures", ...)
  end
  helper(string.find(str, somepattern)

I don't care whether there is only one "..." per function or
one per block.  One per function means every assignment to
"..." within a function would replace the old values, even
when the dots are part of a local statement.  One per block
would mean that you could have local dots.  One thing though:
if one per block will be implemented, then they should also
be accessible as upvalues (complicates things).

Another handy function is argn(...) which simply returns how
many arguments were passed to it.

  print("i have", argn(...), "varargs")

Implemented in C it's trivial, in Lua slow and tricky.

Ciao, ET.