Refutable pattern

Brendan Eich brendan at
Fri Feb 1 12:23:25 PST 2013

Brandon Benvie wrote:
> A postfix '?' would require backtracking when the next '}' is 
> found...I think?

First, let me kill the idea of prefix-'?'.

Prefix-'?' in an AssignmentPattern in an AssignmentExpression that is an 
ExpressionStatement is ambiguous with the '?' in a 
ConditionalExpression, if the programmer mistakenly relies on ASI as if 
prefix-'?' were not used. Consider refactoring ES6 code with prefix-'?' from

    A = B
    {D} = C
L: E

where D is the interior of an object destructuring pattern, to

    A = B
    ?{D} = C
L: E

With postfix-'?' there's no such hazard.

However, naively adding postfix-'?' to the JS grammar does make an 
ambiguity, a shift/reduce conflict on '?' -- shift to keep parsing a 
ConditionalExpression, or reduce to a '?'-suffixed pattern in a 
destructuring assignment.

ES6 draft already splits destructuring binding pattern and assignment 
pattern subgrammars (binding vs. assignment, i.e. var/let/const in front 
vs. no binding keyword). We could therefore forbid postfix-'?' in 
destructuring assignment patterns but not in destructuring binding patterns.

But this is a bit lame, and a refactoring speedbump. I wrote on the 
board the following argument for destructuring assignment to be 
supported along with binding, which MarkM found persuasive:

   function* Fib() {
     let [a, b] = [0, 1];   // destructuring binding
     while (true) {
       yield a;
       [a, b] = [b, a + b]; // destructuring assignment

The two destructurings might want postfix-'?' equally in a different 
function with similar structure.

One solution already used in ECMA-262 to regain LR(1) parsing of the 
standard grammar is lookahead restriction. Observe that with postfix-'?' 
in patterns, the legal lookahead set is {'=', ':' , ',', '}', ']'}. So 
we could simply write a lookahead restriction.

We could factor the grammar harder, but this would duplicate most of the 
expression grammar, akin to how the -NoIn productions duplicate part of 
that sub-grammar. I say let's use a lookahead restriction and get on 
with our lives.

Does anyone see a flaw?


> On Fri, Feb 1, 2013 at 12:21 PM, Andreas Rossberg <rossberg at 
> <mailto:rossberg at>> wrote:
>     On 1 February 2013 10:56, Axel Rauschmayer <axel at
>     <mailto:axel at>> wrote:
>     > Beautiful.
>     >
>     > What do question marks in value (as opposed to key) positions mean?
>     > Example: { a: x? }
>     Not much: a plain identifier 'x' is always matches anyway, i.e. is
>     already irrefutable, so wrapping a '?' around it does not have any
>     effect (it's like writing "if (true)" or whatever). I removed the
>     redundant example.
>     > How does this work grammatically (ternary operator…)?
>     That still has to be worked out. I'd actually prefer a prefixed ?,
>     since it is quite easy to overlook a postfix one trailing a longish
>     pattern when reading code. But that may be more difficult to reconcile
>     with the existing syntax.
>     /Andreas
>     _______________________________________________
>     es-discuss mailing list
>     es-discuss at <mailto:es-discuss at>
> _______________________________________________
> es-discuss mailing list
> es-discuss at
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list