Enforcing arity?

Brandon Benvie brandon at brandonbenvie.com
Mon Jan 7 10:59:32 PST 2013


The outcome of that discussion was here:
https://mail.mozilla.org/pipermail/es-discuss/2012-December/027386.html

I used that solution for a short while, but ultimately I resorted to
trickery to implement it for the ES6 stdlib using an internal
$$ArgumentCount function. The reason is that it's actually observable via
the debugging API I provide what the structure of the params for a
particular function are (this is visible in the debugger), and presenting
an param as rest for the sole purpose of avoiding the arguments object
didn't seem worth it. Still though, it remains possible (if not
particularly graceful) to do this using ...rest without resorting to either
arguments or internal hacks.


On Sun, Jan 6, 2013 at 9:26 PM, Rick Waldron <waldron.rick at gmail.com> wrote:

>
>
> 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 parameter and checking that
>> its length is 0.
>>
>
> I can't find the thread, but a similar question was asked sometime in the
> last 6 months (I think??) and this was the answer that was ultimately
> agreed on as the best way to check arity length.
>
>
> - ensure a minimum arity, by giving y a default value and checking for it.
>>
>
> Explicit undefined arguments, intentional or not, would trigger the
> default value.
>
> Rick
>
>
>>
>> Would fail-fast destructuring work?
>>
>> function add(...args) {
>>     let [x,y] = args;
>> }
>>
>>         --
>> 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/20130107/aeb51220/attachment-0001.html>


More information about the es-discuss mailing list