W3C home > Mailing lists > Public > public-script-coord@w3.org > July to September 2013

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

From: Cameron McCormack <cam@mcc.id.au>
Date: Fri, 27 Sep 2013 15:48:36 +1000
Message-ID: <52451C34.9090600@mcc.id.au>
To: Domenic Denicola <domenic@domenicdenicola.com>
CC: Boris Zbarsky <bzbarsky@MIT.EDU>, Allen Wirfs-Brock <allen@wirfs-brock.com>, Brendan Eich <brendan@mozilla.com>, "public-script-coord@w3.org" <public-script-coord@w3.org>
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

This archive was generated by hypermail 2.3.1 : Tuesday, 6 January 2015 21:37:50 UTC