Default argument values

Steven Mascaro subs at voracity.org
Thu Feb 28 16:07:32 PST 2008


2008/2/29 Brendan Eich <brendan at mozilla.org>:
> On Feb 28, 2008, at 7:29 AM, Lars Hansen wrote:
>
>  > In fact the RI doesn't work as one would like it to, as the third test
>  > below shows:
>  >
>  >>> function h({x:x, y:y} = { x:10, y:20 }) [x,y]
>  >>> h({x:1,y:2})
>  > 1,2
>  >>> h()
>  > 10,20
>  >>> h({x:1})
>  > 1,
>  >
>  > What appears to happen here is what one would expect: if the entire
>  > structure argument is omitted then the default value is used, but
>  > if the
>  > structure is partly omitted then the default structure is not
>  > consulted
>  > at all, and I think that's what Steven would like to see happen.
>
>  Confusion: Steven, do you want to see ES4 do what the RI is doing? I
>  thought you were looking for "member-wise" defaulting, so that the
>  last call, to h({x:1}), would return [1, 20].

My initial concern was 'native' support for optional named arguments.
In the absence of that, I'd indeed like 'member-wise' defaulting.
However, I'm not sure how this affects the language.

Firstly, I can see 2 interpretations of how default values would work
from the end-programmer's perspective:

Step 1. SET arg = default value
Step 2. SET arg = specified value

or:

Step 1. IF no specified value, THEN SET arg = default value

Secondly, destructuring currently works as follows:

>> ({x:a,y:b} = {x:1,y:2}); [a,b]
1,2
>> ({x:a,y:b} = {x:1}); [a,b]
1,
>> typeof(b)
undefined
>>

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})

Of the alternatives, I'd probably be happiest with this kind of operator.



More information about the Es4-discuss mailing list