Exponentiation operator precedence
jason.orendorff at gmail.com
Tue Aug 25 02:04:05 UTC 2015
On Mon, Aug 24, 2015 at 8:10 PM, Waldemar Horwat <waldemar at google.com> wrote:
> That has different right and left precedence and is probably the closest to
> the mathematical intent.
Not to quibble, but I do want to understand:
UnaryExpression : PostfixExpression ** UnaryExpression
AdditiveExpression : AdditiveExpression + MultiplicativeExpression
We don't say binary `+` has different left and right precedence,
right? What's different with `**`, apart from being right-associative?
> However, it does carry other surprises. What does
> each of the following do?
++x ** y; // early error: invalid operand for ++
(because it's parsed as `++(x ** y)`)
x++ ** y; // parses as expected: (x++) ** y
x ** ++y; // parses as expected: x ** (++y)
x ** y++; // parses as expected: x ** (y++)
I'm OK with this. It should be rarer in practice than `-x**2`, and at
least the language won't silently assign a surprising meaning to user
It could be made to parse as `(++x) ** y` by changing prefix ++ and --
to be higher precedence than the other prefix operators. I don't think
this would break any existing programs, because combinations like
`++typeof 3` are already early errors. PHP apparently does something
$ php -r '$x = 3; print(++$x**2 . "\n"); print($x . "\n");'
D doesn't. Python and Haskell don't have ++/--.
More information about the es-discuss