undefined being treated as a missing optional argument

Allen Wirfs-Brock allen at wirfs-brock.com
Fri Apr 13 10:53:24 PDT 2012

On Apr 13, 2012, at 10:10 AM, Brendan Eich wrote:

> Allen Wirfs-Brock wrote:
>> On Apr 12, 2012, at 11:25 PM, Norbert Lindenberg wrote:
>>> >  What about functions that take two or more independent optional arguments, where sometimes you want to omit the first argument while providing the second?
>> arguably, this is where you should be using an options argument rather than multiple positional parameters with default values.  Certainly for the "or more" case.
> Ok, so use an options argument and then add delegation:
> function add({x = 0, y = 0}) {
>  return x + y;
> }
> function inc({opt_y}) {
>  return add({x:1, y:opt_y});
> }
> assertEq(1, inc({}));
> The author surely wants y to default to 0 and for the assertEq to succeed.
> In general, delegation (depth D) plus optionality (degree N paramters) makes an (2N)^D worst-case combinatorial explosion.
> This is IMHO a strong argument for a sentinel in-language to mean "missing actual".

That sentinel could simply be a empty argument position:

          new Intl.Collator( , {usage: "search"});

An implementation level sentinel value would probably still be needed but it would never be directly exposed to user level code.

I was aghast the first time somebody suggested this a while ago.  It reminded me to much of OS/360 JCL:

    //U99999A JOB (*),,CLASS=A 

However if there is a real need for non-trailing defaulting positional parameters then that may indeed be the best way to do it.  If so, I would define it so that something like:
     function f(...args) {}

produced a sparse args array object. This would actually straighten the common semantics between formal parameter processing and array destructuring.

It also have to apply and argument list result spread translate array holes into the internal missing value sentinel. 


More information about the es-discuss mailing list