Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upExponentiation syntax sugar #172
Conversation
SiegeLord
added some commits
Jul 19, 2014
lifthrasiir
reviewed
Jul 19, 2014
| ### Precedence | ||
|
|
||
| The precedence should be above multiplication, but below casting. That is, the | ||
| following pair of assignments are equivalent: |
This comment has been minimized.
This comment has been minimized.
lifthrasiir
Jul 19, 2014
Contributor
How about the associativity? The right-associative power operator is common, but there seems to be some cases that the power operator is left-associative so the clarification is needed.
This comment has been minimized.
This comment has been minimized.
bill-myers
Jul 19, 2014
I suggest just making it an error to have an expression where associativity makes the difference.
This simplifies the language, and it should be very rare anyway.
It's also possible to later change to left or right associativity without breaking compatibility.
This comment has been minimized.
This comment has been minimized.
SiegeLord
Jul 19, 2014
Author
I just checked, and in every language I tried this operator is right-associative. Is it a big deal to just make that be the rule? This wouldn't be the first binary operator in Rust that is right-associative (= is, although it is not overloadable). The IndexSet operator from the Indexing RFC would also probably to be right associative.
This comment has been minimized.
This comment has been minimized.
lifthrasiir
Jul 19, 2014
Contributor
I do not think that the left-associative power operator is a good choice, but I do think that the choice of associativity needs a conscious decision. For the reference, the PHP RFC has a list of languages with the left-associative and right-associative power operator.
This comment has been minimized.
This comment has been minimized.
glaebhoerl
Jul 20, 2014
Contributor
In usual mathematical notation it's right-associative:
Without parentheses to modify the order of calculation, by convention the order is top-down, not bottom-up:
I think this is a compelling argument in favor of following the same convention.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
gsingh93
commented
Jul 20, 2014
|
+1 to the idea in general. Having a power operator definitely isn't necessary, but it's one of those things that you just appreciate about the language. I'd prefer |
This comment has been minimized.
This comment has been minimized.
Is there really any grammatical conflict with double-dereference, given that one is a unary prefix and the other a binary infix operator? We have some instances of the same token being used in both prefix and infix positions ( The fact that If not for the above I would prefer |
This comment has been minimized.
This comment has been minimized.
What I'm referring to there is this currently valid code: let a = &&1u;
let b = **a;If
The issue to me is not that it's ambiguous (I don't think it is, it'll be parsed as exponentiation in all cases), it's that it breaks existing code. There also might be a possibility where |
SiegeLord
added some commits
Jul 20, 2014
This comment has been minimized.
This comment has been minimized.
It's ambiguous in the sense that it could be intended to mean different things, even if the compiler resolves it by always taking it to mean one of them and not the other. It may or may not be the one the programmer intended.
Yes, this is the possibility which is worrying to me. I don't think it's possible with anything in the standard library, but with e.g. third-party numeric libraries, I think it's easily within the realm of possibility. All you need is |
This comment has been minimized.
This comment has been minimized.
pczarn
commented
Jul 20, 2014
|
If the user cares only about commutative multiplication and not about overloaded operators, (s)he can write |
This comment has been minimized.
This comment has been minimized.
|
The idea is that the potential dual meaning of |
This comment has been minimized.
This comment has been minimized.
pczarn
commented
Jul 20, 2014
|
I see. It could have been forbidden from the beginning, so that a final decision on exponentiation could be made before 1.0. |
This comment has been minimized.
This comment has been minimized.
|
I absolutely agree that |
This comment has been minimized.
This comment has been minimized.
|
The consensus of the team on this subject is that |
brson
closed this
Aug 1, 2014
This comment has been minimized.
This comment has been minimized.
|
Aww, sad. Well, I'll know who to blame for all the math libraries overloading |
This comment has been minimized.
This comment has been minimized.
|
For what it's worth, I suggested XOR get a new Not that it matters. Oh well. |
This comment has been minimized.
This comment has been minimized.
pczarn
commented
Aug 1, 2014
|
@mdinger Bitwise operators have one character, for example |
This comment has been minimized.
This comment has been minimized.
|
@pczarn I'm aware of the comparison:
I just thought it was nifty. |
This comment has been minimized.
This comment has been minimized.
pczarn
commented
Aug 1, 2014
|
Quite nifty, though it might be confused with the |

SiegeLord commentedJul 19, 2014
Summary:
Add a new binary operator for exponentiation. This involves two additions to the
language:
expr ** exprorexpr ^^ expr)PowRendered