destructuring: as patterns?

Andreas Rossberg rossberg at google.com
Fri Apr 20 10:38:36 PDT 2012


On 20 April 2012 17:53, Allen Wirfs-Brock <allen at wirfs-brock.com> wrote:
>
> 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?

Maybe we are talking past each other, but why do you say : isn't used
in expressions? It's part of the syntax of object literals, and I
think it should keep the same meaning in patterns. On the other hand,
you have an expression form 'e1 = e2', where all free variables are
use-sites. Analogously, in patterns you have 'p1 as p2' (at least if
you think of the most general form), where all free variables are
binding sites (because that's the very nature of a pattern).

What's left of : in an object literal is not a variable, in neither
expressions nor patterns. That's why I think you shouldn't conflate :
and 'as', neither one way (Dave's suggestion) nor the other (your
suggestion).

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

OK, I thought you wanted to tie it to BindingPropertyList. Still,
BindingElement seems to be the wrong place. According to the current
grammar, it would not allow me to write any of

  let x as {a, b} = bla
  for (let x as {a, b} of bla) ...
  {set c(x as {a, b}) {...}}
  try { ...} catch (x as {a, b}) { ... }

That is, the canonical place in the grammar would be BindingPattern AFAICS.

> or, since we are making up syntax:
>
>      let mom : dad : auntie = peopleComstants.unidentifiedAdult;
>
> or
>
>      let (mom, dad, auntie) = peopleComstants.unidentifiedAdult;

The latter looks far too much like a tuple, and would be particularly
confusing in a parameter list, I'd say.

/Andreas


More information about the es-discuss mailing list