Enforcing arity?

Herby Vojčík herby at mailbox.sk
Sun Jan 6 17:07:44 PST 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 parameter and checking that
> its length is 0.
> - ensure a minimum arity, by giving y a default value and checking for it.
>
> Would fail-fast destructuring work?
>
> function add(...args) {
> let [x,y] = args;
> }

And it this would be possible, it could be in the signature:

   function add(...[x,y,opt?]) {
   }

> --
> Dr. Axel Rauschmayer
> axel at rauschma.de <mailto:axel at rauschma.de>

Herby


More information about the es-discuss mailing list