arguments or splats

48 views
Skip to first unread message

Jake Verbaten

unread,
Mar 24, 2012, 4:23:17 PM3/24/12
to cando...@googlegroups.com
How do we handle functions with arbitary arguments?

Jake Verbaten

unread,
Mar 24, 2012, 4:37:34 PM3/24/12
to cando...@googlegroups.com
I tried using the es harmony spread and rest ... syntax in this example


this my js library ported verbatim to candor

Tim Caswell

unread,
Mar 24, 2012, 5:48:25 PM3/24/12
to cando...@googlegroups.com
I kinda like the rest/splat syntax.  I've used ... from lua a lot so that's why I used that in my OOP example, but I like how "...foo" is just a special array.

So for candor, I propose "...name" to consume all extra arguments and put them in the array argument "name".  And for calling, "...name" expands the array "name" in place as multiple arguments.

I'm not sure I want to restrict it to being at the end of the arguments list like ES is doing.  In node-style async functions, for example, the optional arguments are often in the middle with a callback at the end.

I guess the real question is how can Fedor implement this without killing the simplicity and performance of the function calling code.

Eventually I think argument destructuring would be good too.  That might even be enough to replace the need for varargs in every case except for meta libraries (OOP frameworks, etc..)

Having both destructuring and ...foo syntax is powerful for optional args.

readFile = (path, ...options, callback) {
  [encoding, maxSize, other] = options
  // now encoding, maxSize, and other are initialized with the right arguments and nil if they didn't exist.
}

Without destructuring, it's still not bad
readFile = (path, ...options, callback) {
  encoding = options[0]
  maxSize = options[1]
  other = options[2]
  // ...
}

Or if we allowed destructuring in the formal parameters list itself

readfile = (path, ...[encoding, maxSize, other], callback) {
}

Or this example from the harmony destructuring page that has object and array destructuring and ... in the parameter list.

function f( { "name": n }, ...[ a, b, c ] )
{
}

Fedor Indutny

unread,
Mar 25, 2012, 11:02:12 AM3/25/12
to cando...@googlegroups.com
Implemented (including vararg in the middle), please review:

P.S.

Candor example (and test) is on the bottom

Cheers,
Fedor.
Reply all
Reply to author
Forward
0 new messages