Re: Reconciling handling of optional arguments and handling of default values across ES and webidl

Cameron McCormack:
>> Yes.  Since arg2 must be specified, I think it makes more sense to have length be 2 rather than 0.  WDYT?

Domenic Denicola:
> This does not match ES6, where it would be zero if you wrote
>
> function foo(arg1 = undefined, arg2) {
>    if (arg2 === undefined) throw new TypeError("arg2 required");
> }
>
> I suppose you could not map "optional" to "= undefined", but that seems most natural, and fits well with how I assume default arguments in WebIDL should be mapped to ES.

It's reasonable to think of "optional" with no default value being 
equivalent to "= undefined" in JS, you're right.  I actually was going 
to argue for that -- and for length to be 0 in this case -- in my reply 
to Boris before I saw this in the ES6 spec:

   The value of the length property is an integer that indicates the
   typical number of arguments expected by the function.

Since we know that it's never valid to call the function with 0 
arguments, why would we have length = 0?  This is not something that 
comes up in JS, since the logic of requiring the second argument has to 
be within the function body.

But maybe that's not relevant, and instead we should just be looking at 
the most natural way of implementing the IDL operation as a JS function, 
and just letting length fall out of that.

   void f(long x);
       =>  function f(x) { }
       =>  .length = 1

   void f(optional long x);
       =>  function f(x = undefined) { }
       =>  .length = 0

   void f(optional long x = 0);
       =>  function f(x = 0) { }
       =>  .length = 0

   void f(optional long x, long y);
       =>  function f(x = undefined, y) { }
       =>  .length = 0

The alternative is:

   void g(optional long x);
       =>  function g(x) { }
       =>  .length = 1

   void g(optional long x = 0);
       =>  function g(x = 0) { }
       =>  .length = 0

which is weird, since it's completely fine to call g without its 
argument, so why shouldn't length be 0?

So I think I'm persuaded to go with 0 here. :)

Received on Friday, 27 September 2013 05:49:25 UTC