destructuring: as patterns?

Allen Wirfs-Brock allen at wirfs-brock.com
Fri Apr 20 08:53:59 PDT 2012


On Apr 20, 2012, at 2:16 AM, Andreas Rossberg wrote:

> 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.

I don't understand.  : isn't used in expressions and the productions to the right of an = doesn't introduce any bindings.  The : as syntax seems to contain identifier bindings in both what follows : and what follows as.  How is : and as different other than which binding forms are allowed after each?


> 
> 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.
> 

What's not compositional about the above?  I'm not going to quote the whole BNF from the draft specification but the key productions are:

BindingPattern :
    ObjectBindingPattern
    ArrayBindingPattern

ObjectBindingPattern :
    { BindingPropertyList }

ArrayBindingPattern :
    [ BindingElementList ]

BindingProperty :
    PropertyName : BindingElement

BindingElement :
    BindingIdentifier
    BindingPattern

All I was saying is that references to BindingElement in that grammar could be replaced with Bindings defined as

Bindings :
   Bindings : BindingElement
   BindingElement

Still completely compositional.  It is simply permitting a single value from the source object to be used as the initializer of multiple bindings.  This could work anywhere, include argument lists.


> 
>> 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'.

You're right, I made the mistake of thinking that the assignments in the let initializer were binding forms.  They aren't. I'm sure others will make this same mistake.  It makes me want to restrict assignment operators in let/const initializers!


> 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;

or, since we are making up syntax:

      let mom : dad : auntie = peopleComstants.unidentifiedAdult;

or

      let (mom, dad, auntie) = peopleComstants.unidentifiedAdult;


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

or something...


> 
> /Andreas
> 



More information about the es-discuss mailing list