Enforcing arity

Allen Wirfs-Brock allen at wirfs-brock.com
Sat Mar 16 17:17:19 PDT 2013


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) {
       if (args.length <3) throw Error("too few arguments");
       let [[a,b], c,d=5, ...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)?
> 
> 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 `!` matching operator, the inverse of postfix `?`:
> function foo(required!, optional) { ... }
> 
> -- 
> Dr. Axel Rauschmayer
> axel at rauschma.de
> 
> home: rauschma.de
> twitter: twitter.com/rauschma
> blog: 2ality.com
> 
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130316/e6715586/attachment-0001.html>


More information about the es-discuss mailing list