"More arguments for a function on request" (Variadics)

On Red's Gitter, @GiuseppeChillemi asked:

Do you think it would be possible to get more arguments for a function on request?

Example:

aaa: 1
bbb: 2
ccc: 3
f: func [arg1] [other-args: get-args 3]
f arg1-val aaa bbb ccc

And Gregg says "oh that would make Red unsafe" (as if Red were somehow "safe").

Anyway, Ren-C has some support for variadics. While the implementation hasn't gotten a lot of attention in a while, it's no less safe than anything else--the mechanisms used are the same ones that gather ordinary arguments.

 aaa: 1
 bbb: 2
 ccc: 3
 f: func [arg1 others [<variadic> any-value?]] [
     return collect [keep arg1, repeat 3 [keep take others]]
 ]

 >> f 1020 aaa bbb ccc
 == [1020 1 2 3]

The interface is probably not what it's going to ultimately be. But for now, you can mark a parameter <variadic> and it will be a VARARGS!, that supports TAKE to acquire parameters after the call is made.

Variadics should be used sparingly, because they are difficult to wrap and abstract. There are some mechanisms for APPLY'ing variadics using a BLOCK! to supply the variadic arguments, but the interface for that is also likely to change.

1 Like