fail-fast object destructuring

Russell Leggett russell.leggett at gmail.com
Wed Jul 11 08:20:07 PDT 2012


On Wed, Jul 11, 2012 at 7:14 AM, Andreas Rossberg <rossberg at google.com>wrote:

> On 10 July 2012 10:17, Russell Leggett <russell.leggett at gmail.com> wrote:
>
>> Yes, I think that arrays are likely the strongest case for ? on bindings.
>> Something like this would be nice:
>>     let [first,last,?company] = contact.split(",");
>>
>
> Actually, I don't see how this is a case of "? on bindings". In fact, the
> '?' would have to be interpreted as part of the array pattern syntax for
> the above to make sense (in analogy to object patterns, it happens to be
> associated with the implicit property name "2").
>

Well, I mean, if you could put it on {}, [], and on variable names, it
would have to be part of the expression cover grammer as Brendan was
talking about. I suppose technically I should have said "? on expressions
that can be interpreted as patterns" or something.

In the array case, I guess it would be the equivalent of

    let {0:first, 1:last, ?2:company} = contact.split(",");

Although that highlights an interesting point about object patterns,
perhaps it was already covered. If the property is not a valid identifier,
it could not use the property shorthand form. This should be an early error.

Also related, is there any structural requirement for the value being
matched in the case of a [] pattern? If you use ..., there is a requirement
to support length.


>
> AFAICS, '?' on a variable itself would always be redundant, because a
> variable pattern is irrefutable anyway.
>

So you're saying that even this should match in a refutable pattern:

   let [a,b,c] = [1,2];

I would expect that to fail, especially in a hypothetical pattern matching
construct.

- Russ


>
> /Andreas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120711/1e40d704/attachment.html>


More information about the es-discuss mailing list