-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Priority of unary -
relative to **
#12999
Comments
This was already discussed in the past: #9684 We made some changes back then so it's unlikely we'll make them again. There's more discussion here: https://forum.crystal-lang.org/t/exponentiation-operator-has-unexpected-behaviour/2457 |
Or maybe it was a separate discussion. I think given that 1.0 is out this is unlikely to change. |
AFAIK there were concerns about the parser complexity because number literals like Until at least Crystal 2.0 (and probably even afterwards), this should stay as-is. Changing the operator precedence post-1.0 will introduce lots of subtle breaking changes, without any useful compiler messages. |
A much more softer measure we can introduce without any hassel: The formatter could apply parenethesis for this. |
Both is best 🙂 |
I'd like to avoid having the compiler spit out excessive warnings. There's not much point in adding warnings for anything the formatter would reformat. |
What aspect of the language would you like to see improved?
Change operator precedence, so
**
will have higher priority then unary-
(and perhaps over other unary operators).What are the reasons?
In most languages I've checked (Ruby, Python, Julia, Octave, VB6)
-2**2
gives -4 (operation**
have higher priority than unary-
). This is IMHO consistent with mathematical notation (-22 = 4). Excel (and VBScript\Google Sheets) returns4
though, so there is no universal consensus.Include practical examples to illustrate your points.
More complete list of languages (compiled from here and my own checks):
-2 power 2 gives -4
: Ada, AWK, Haskell, J, Julia, langur, Lua, Maple, Mathematica / Wolfram, ExtendedPascal, R, Raku, Ruby, Python, Octave/MATLAB, BASIC(most, but not all dialects), DLang, PHP, VB.NET-2 power 2 gives 4
: ALGOL 68, F#, Factor, Nim, REXX, Crystal, VBScript/Excel, Smalltalk (but it hasn't unary operators actually)explicitly require parenthesis in -2**2
: JavaScriptOptionally add one (or more) proposals to improve the current situation.
In Crystal,
-2**2
gives4
, this is documented, but i wonder if there is any motivation behind this decision or just coincidence.JavaScript approach looks appealing to me, but I'm not sure how it fits the rest of language.
Of course, changing it at this stage would be a breaking change for minimal profit, but i haven't found corresponding issue, so I think there should be one. If consensus is to keep current behavior, maybe this worth noting in a
Differences from Ruby
.The text was updated successfully, but these errors were encountered: