New varargs

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

New varargs

Edgar Toernig
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.

Reply | Threaded
Open this post in threaded view
|

Re: New varargs

Mark Hamburg-4
I agree that both of those would be useful. Capturing varargs on assignment
is particularly interesting for the results of a pcall if you want to pass
the results on up the chain.

Mark

on 7/1/04 4:56 PM, Edgar Toernig at [hidden email] wrote:

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

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


Reply | Threaded
Open this post in threaded view
|

Re: New varargs

Ben Sunshine-Hill
Of course, what that really is, is a prelude to tuples as first-class
types. Perhaps we should reevaluate the relation between tables and
tuples?

On Thu, 01 Jul 2004 17:02:02 -0700, Mark Hamburg <[hidden email]> wrote:
> 
> I agree that both of those would be useful. Capturing varargs on assignment
> is particularly interesting for the results of a pcall if you want to pass
> the results on up the chain.
> 
> Mark
> 
> 
> 
> on 7/1/04 4:56 PM, Edgar Toernig at [hidden email] wrote:
> 
> > local start, end, ... = string.find(str, somepattern)
> > print("captures", ...)
> 
> > print("i have", argn(...), "varargs")
> 
>