lua-users home
lua-l archive

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


Peter Hill
> This is enhanced to allow:
>     a, b, ... = f()
> This simply assigns "arg" just like in a parameter assignment.

Edgar Toernig:
> This is on my wishlist for a long time ;-)  But at the same time I
> would remove the arg table.

Syntactically disposing of the arbitrary "arg" name and using "..." instead
certainly makes sense to me! (Now we just need a symbol to replace the
"self" word :-).

An alternate is to give a user-defined name. Eg:
    function f(a, b, ...c)      (AND)     a, b, ...c = g()

so the current default behaviour is:
    function f(a, b, ...arg)


> Instead, '...' can also be used as a value that evaluates to all optional
> arguments.  If you really need an arg table you can build it with "local
> arg={...}". The advantage would be that the VM does not have to build a
> table when it is not needed.  At the moment, vararg functions trash the
> memory pool with short living tables...

Now this is rather interesting.

This suggestion, replacing the "arg" Table with a pseudo-function that
returns the optional arguments seems rather efficient... though still a bit
limited. How about if I enhance "arg()" a bit more:
- arg() -- returns the number of optional parameters.
- arg(n) -- returns the nth parameter, where n can be +ve or -ve.
- arg(n,m) -- returns (m-n+1) parameters : arg(n), arg(n+1), ..., arg(m)

This gives you a lot of flexibility, and is very efficient for the compiler.
However, not being a real function, it requires "arg" to be a keyword... or
creating a new argfunc type... or the use of "...".

So, using your examples, we have:
   function foo(...)
     a(stdout, arg(1,-1))
     b{arg(1,-1)}

   function bar(a,...)
     if a=="line" then
       local x1,y1,x2,y2 = arg(1,4)
       xxx
     elseif a=="circle" then
       local x,y,r = arg(1,3)
       xxx

An alternate of the "line"/"circle", to demonstrate progressive use of
optional parameters is:
   function bar(...)
     if arg(1)=="line" then
       local x1,y1,x2,y2 = arg(2,5)
       xxx
     elseif arg(1)=="circle" then
       local x,y,r = arg(2,4)
       xxx
     xxx

*cheers*
Peter Hill.