what is the syntax of DOTS?

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

what is the syntax of DOTS?

Mark Ian Barlow
Looking at parser.c I spotted the symbol DOTS ('...'). I guessed it was a
valid function parameter, and sure enough:

function foo(x,...) print(x) end   foo("bar")

was accepted, but (assuming the obvious) how do I access the parameter list
within the body of foo()? Should I be doing this at all or is it a legacy
from an earlier version, or maybe an internal reserved feature?

--  Mark Ian Barlow                Non-Linear Control Consultants Ltd.
    -----------------------------------------------------------------
    [hidden email]            Voice / Fax: +44 (0)1207 562 154

Reply | Threaded
Open this post in threaded view
|

Re: what is the syntax of DOTS?

Luiz Henrique de Figueiredo-2
>Looking at parser.c I spotted the symbol DOTS ('...'). I guessed it was a
>valid function parameter, and sure enough:
>
>function foo(x,...) print(x) end   foo("bar")
>
>was accepted, but (assuming the obvious) how do I access the parameter list
>within the body of foo()? Should I be doing this at all or is it a legacy
>from an earlier version, or maybe an internal reserved feature?

This is an experimental, unreleased implementation of variable argument lists.
It may become official in the next version. Or it may not :-)
If you look closer at the code, you'll find a function 'call' that is supposed
to be used with this.
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: what is the syntax of DOTS?

Roberto Ierusalimschy
Being more specific about variable argument lists:

A function declared like "function f(a1, a2, ...)" has, besides a1 and a2,
an extra 'parameter' called "arg". The value of "arg" is always a table,
which has in fields 1,2,... the values of any extra arguments passed to
f, and a field 'n' with the number of extra arguments. For instance:

function f (a1, ...)
  -- control point xx
end

f(3, 5, 7)  -- at point xx, a1=3, arg={5, 7; n=2}
f()         -- at point xx, a1=nil, arg={n=0}
f('a')      -- at point xx, a1='a', arg={n=0}

There is also a pre-defined function "call", that receives a function and
a table, and calls the given function with the parameters given in the
table. More specifically, the expression

  call(f, {a1, a2, [...], an})    -- the [...] is meta-language

is equivalent to

  f(a1, a2, [...], an)  

And a declaration like

function f(...) return call(g, arg) end    -- the ... is language

makes 'f' equivalent to 'g'.


  These facilities are fully implemented in version 2.5, but still
undocumented. Probably they will become oficial in next release
(but please note, "probably").

-- Roberto