Enforcing arity

2013-03-16 Thread Axel Rauschmayer
What is the recommended way of enforcing arity under ECMAScript 6 (while avoiding arguments.length)? Two ideas: 1. Parameter default value is a function call that throws an exception. Not sure if that works. function foo(required = throwAnException(), optional) { ... } 2. Introduce a postfix

Re: Enforcing arity

2013-03-16 Thread Allen Wirfs-Brock
, ...rest] = args; //probably need some ?'s in there, but I haven't internalized the details of the new pattern matching yet. } Allen On Mar 16, 2013, at 5:02 PM, Axel Rauschmayer wrote: What is the recommended way of enforcing arity under ECMAScript 6 (while avoiding arguments.length

Re: Enforcing arity

2013-03-16 Thread Axel Rauschmayer
On Mar 17, 2013, at 1:17 , Allen Wirfs-Brock al...@wirfs-brock.com wrote: If you need to do arity analysis of parameter but also what to apply default values, destructuring, etc I would do the following: Instead of function ([a,b], c,d=5, ...rest) {...} do function (...args) {

Re: Enforcing arity

2013-03-16 Thread Kevin Gadd
Would using (...args) incur a performance penalty and impair optimization since the argument list has to be an array now? Or is that still better than using 'arguments.length'? Enforcing arity is a common enough (and important, IMO) pattern that I'd be wary of doing it using a pattern

Re: Enforcing arity

2013-03-16 Thread Allen Wirfs-Brock
. issue, then it really doesn't matter. Allen On Mar 16, 2013, at 5:27 PM, Kevin Gadd wrote: Would using (...args) incur a performance penalty and impair optimization since the argument list has to be an array now? Or is that still better than using 'arguments.length'? Enforcing arity

Re: Enforcing arity?

2013-01-07 Thread Jason Orendorff
On Sun, Jan 6, 2013 at 6:31 PM, Axel Rauschmayer a...@rauschma.de wrote: What is the simplest way of enforcing an arity in ES6? Doesn’t it involve arguments? Hmm. Can you do this? function f(x, y, ...[]) {} -j ___ es-discuss mailing list

Re: Enforcing arity?

2013-01-07 Thread Brendan Eich
Jason Orendorff wrote: On Sun, Jan 6, 2013 at 6:31 PM, Axel Rauschmayer a...@rauschma.de mailto:a...@rauschma.de wrote: What is the simplest way of enforcing an arity in ES6? Doesn’t it involve arguments? Hmm. Can you do this? function f(x, y, ...[]) {} The question is, with

Re: Enforcing arity?

2013-01-07 Thread Axel Rauschmayer
I’m torn: On one hand, I love the cleverness of it and it is certainly a good solution (and one that doesn’t necessitate introducing new language features). On the other hand, I’m wondering if it wouldn’t be cleaner to have a per-function flag that tells JavaScript to enforce the specified

Re: Enforcing arity?

2013-01-06 Thread Herby Vojčík
Axel Rauschmayer wrote: What is the simplest way of enforcing an arity in ES6? Doesn’t it involve arguments? function add(x, y) { if (arguments.length !== 2) throw ... } To avoid `argument`, one could: - ensure a maximum arity by adding a ...rest parameter and checking that its length is 0.

Re: Enforcing arity?

2013-01-06 Thread Rick Waldron
On Sunday, January 6, 2013, Axel Rauschmayer wrote: What is the simplest way of enforcing an arity in ES6? Doesn’t it involve arguments? function add(x, y) { if (arguments.length !== 2) throw ... } To avoid `argument`, one could: - ensure a maximum arity by adding a ...rest