Default argument values

Lars Hansen lhansen at adobe.com
Fri Feb 29 02:30:43 PST 2008


> -----Original Message-----
> From: voracity at gmail.com [mailto:voracity at gmail.com] On 
> Behalf Of Steven Mascaro
> Sent: 29. februar 2008 01:08
> To: Brendan Eich
> Cc: Lars Hansen; es4-discuss at mozilla.org
> Subject: Re: Default argument values
> 
> 
> Suppose we took the first interpretation of default values and object
> destructuring were changed so that omitted (or undefined) members
> weren't assigned at all. Only then would a result in which calling
> h({x:1}) returns [1, 20] be consistent with the rest of the language.
> A third interpretation would involve treating destructuring defaults
> differently to other defaults, but I don't like that kind of special
> casing (from an end-programmer's perspective).
> 
> As mentioned, an alternative is an operator for defaults in
> destructuring. Lars, I assume you meant by this the following (in
> shorthand):
> 
> function h({x ?= 10, y ?= 20}) [x,y]
> 
> or in longhand in a statement:
> 
> ({x:x ?= 10, y:y ?= 20} = {x: 1})

What I meant was an assignment operator so that I could write:

  { xProp: x, yProp: y } ?= { xProp: 10, yProp: 20 };

and x and y would be overwritten only if their values were undefined.
C# has a similar operator for conditional assignment which assigns if
the lhs value is null (??= or something like that, I'm offline and
away from my bookshelf right now).

How this fits into default arguments I don't know for sure.  I
superficially imagined that ?= would replace = in the parameter list:

  function f({ xProp: x, yProp:y } ?= { xProp: 10, yProp: 20 }) ...

The meaning of that should be intuitive and requires fairly lightweight
changes only.

Lest I get myself into another "goto" problem, I'll make it clear that
I'm not advocating this for ES4.  We're IMO past all deadlines for
new design.  I'm just thinking out loud, for the benefit of ES5 :-)

--lars



More information about the Es4-discuss mailing list