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
, ...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
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) {
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
. 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
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
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
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
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.
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
10 matches
Mail list logo