-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Non-transitive operator precedence #18714
Comments
I think that would just make the precedence rules even more confusing; is there any extant language that has ever defined precedence rules in this way? |
It certainly has the potential to do so when abused. But on the other hand, it would also allow cases that are confusing at the moment to be undefined and require explicit parentheses. Then, instead of unexpected behavior, one gets an error right away. Working out a set of rules that are generally perceived as least confusing would certainly be no easy feat, though. |
The problem with implementing complicated DWIM parser rules is that no one can remember exactly what they are, so you end up having to add explicit parentheses anyway if you want to be sure of what your code is doing. |
Relevant LtU thread on alternatives to operator precedence rankings. Two language design choices of note:
|
There are two separate issues to be discussed here.
I think the former is a good idea since it would immediately prevent common bugs and errors. The latter seems far more questionable and a really convincing case would need to be made for it. |
A weak +1 to Stefan's first point (weak because it's a good idea and could encourage better practices but it may be annoying for people who know exactly what they're doing), and a strong -1 to the second. I think that has the potential to cause far more confusion than it would solve. |
There may be important theoretical arguments against non-transitive operator precedence, but IMO I find it unwieldy to have to write issome(x) && (return x)
iscond(x) && (y = x)
anothercond(x) && (return throw()) I feel like that should just work, since it's so unambiguous, regardless or raised theoretical arguments against. |
You don't need parentheses around |
I see that the proposed mechanism might be badly abused. I'm with @StefanKarpinski in #18714 (comment) here, making ambiguous cases an error is IMHO beneficial, adding fancier rules should be done with great care, if at all. Allowing has_no_proper_value(x) && x = default_value looks relatively innocent, but even here, I wouldn't rush things. I just thought that if precedence undergoes a revision, we might just as well do something which would be very flexible, even if we don't exploit all that flexibility immediately. BTW, why does |
I think the intention with |
I think the
which already has the right precedence for that. There is an issue for this somewhere. |
@JeffBezanson The syntax you mentioned would require a change to how ternaries are parsed, wouldn't it? Since it's a ternary with the |
Yes the |
Ref: #5187 (comment) and the following discussion
I couldn't find a specific issue for this, but it seems worthwhile to have one.
Conceptually, I imagine this to work by having a (non-total) mapping from (ordered) pairs of operators to the set {Left, Right}, which indicates the operator of higher precedence. E.g.
ensures that
-
is left-associative while^
is right-associative andwould allow that
a = b && c
parses asa = (b && c)
whilea && b = c
parses asa && (b = c)
. If no mapping is defined for a combination, the result is a parse error, requesting explicit parentheses.The text was updated successfully, but these errors were encountered: