Precedence of yield operator

Brendan Eich brendan at mozilla.com
Fri Jun 14 11:07:48 PDT 2013


Bruno Jouhier wrote:
> While playing with my little async/await library, I noticed that I was 
> often forced to parenthesize yield expressions as (yield exp) because 
> of the low precedence of the yield operator. Typical patterns are:
>
> var foo = (yield a()) + (yield b()) + (yield c());

That's actually a hard case, IMHO -- and "hard cases make bad law".

Many programmers would rather have the extra parens for uncertain cases 
(C's misplaced bitwise-logical and shift operators, vs. 
equality/relational; anything novel such as yield).

But the real reason for yield being low precedence is to avoid 
precedence inversion. Consider if yield could be high-precedence, say a 
unary operator like delete:

let x = yield a + b;

Oops, many people would want that to be equivalent to

let x = yield (a + b);

but if yield is at delete's precence level, it's rather:

let x = (yield a) + b;

Which is the rare ("hard", from law school) case.

For commutative operators such as +, over-parenthesizing is better 
again, because

let x = b + (yield a);

and

let x = (yield a) + b;

ignoring order of effects in shared mutable store, and ignoring floating 
point non-determinism, are equivalent.

/be


More information about the es-discuss mailing list