destructuring: as patterns?

Andreas Rossberg rossberg at google.com
Fri Apr 20 02:16:03 PDT 2012


On 20 April 2012 02:15, Allen Wirfs-Brock <allen at wirfs-brock.com> wrote:
> On Apr 19, 2012, at 4:16 PM, David Herman wrote:
>>    let { unidentifiedAdult: mom as dad, unidentifiedChild: sister as brother }
>>
>> or
>>
>>    let { undentifiedAdult as mom as dad, unidentifiedChild as sister as brother }
>>
>
> I actually  I found
>      propertyName :  bindingIdentifier as bindingPattern
>
> (do I have the subparts right?) somewhat confusing at first and I think other might also.  What is the difference between : and |as|?

Essentially the same as the difference between : and = in expressions.

Having said that, I don't like Dave's suggestion of overloading 'as'
for object destructuring. I agree that object patterns may be
confusing at first, but you also get used to that quickly. And
overloading 'as' is probably even more confusing, as this discussion
already shows.


>  Why are different things allowed after each?  Only one |as|?  I actually think your second alternative is clearer but why not:
>
>  let {unidentifiedAdult : mom : dad : auntie, unidentifiedChild : sister : brother} = peopleComstants.

*gulp*

Not sure if that was a serious suggestion, but let me emphasize again
that layered patterns need to be _compositional_, because you want to
be able to use them in other places, e.g. parameter lists. Tying them
to one particular, unrelated destructuring construct is not right.


> If you think about destructuring simply as an alternative initialization syntax that seems to make more sense.  Essentially : and = are duals that operate in opposite directions:
>
>       let {unidentifiedAdult : mom : dad : auntie} = peopleComstants;  //right to left initialization
> vs
>       let mom = dad = auntie = peopleComstants. unidentifiedAdult;  //left to right initialization

I disagree. That is a bogus analogy. An object pattern is completely
different from a 'let'. In particular, the latter binds 'mom', but not
'dad' and 'auntie'. The proper equivalence (I wouldn't call it a
duality) would be

       let {unidentifiedAdult : mom as dad as auntie} = peopleComstants;
vs
       let mom as dad as auntie = peopleComstants.unidentifiedAdult;

You cannot even express it without a compositional 'as'.

/Andreas


More information about the es-discuss mailing list