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
operator precedence enforcement #5074
Comments
In crystal, it parses as |
Sure. Please write down the algorithm to implement this. |
what algorithm do you mean exactly? |
I mean, you propose that the compiler enforces parentheses where it would be otherwise ambiguous for a user to understand it. For that to work, the compiler has to have a formal algorithm for this, or maybe a bunch of heuristics. We don't have time to develop this, and I personally think that if it's too confusing to read, just add parentheses, it's not that terrible. No other programming language that I know of enforces parentheses. So if you want to see this happen you'll have to write the code or the algorithm that implements this. But if you don't have time for this, I think we can close this. |
this was the proposed ordering:
same goes for formatting code, if it is not formatted well, I just format it well but if there is nobody who can work this out in practice than you can probably close it, agreed |
I'm pretty sure we are never going to implement this, so I'm closing this. For example, is this confusing?
Maybe for some it is, if they don't know that
I don't know, the first one was probably easy to understand. The same probably applies to other operators. And in doubt, just use parentheses to make sure you get the correct association. |
like in the blogpost
would work, it is not about enforcing brackets on every operator but mainly between classes of operators |
OK, I'll reopen this. Maybe some day someone will implement this... |
keep faith ;) |
So I made some more examples, tried to come of with more explicit rules. sorted by strength of binding: very strong to weak1. member access and function calls
2. Math und Binary ops
3. comparison ops
4. logical ops
=
no mixed chaining rules
examples
examples of all allowed combinationslocigal <-> comp
bin_math <-> comp
bin_math <-> logical
all the following work:because where
|
Just a small note: A logical operator should have higher precedence than a comparator, making |
thanks, I updated the notes accordingly |
I would support this if it was a PR. Not sure if we'll have time to implement this for a long time though. |
I would see this as a feature for the formatter because it's just a formatting issue. The parser doesn't need parenthesis to disambiguate because the precedence rules are clear, so it doesn't need to enforce it. It only helps humans to follow the code better, so that's a job for the formatter. As long as it's not too intrusive, this should be fine. |
PREFACE: I have searched and not found an issue about considerations that may have already been made about this, if I missed the issue I'd close this one.
blogpost - reasoning behind this
in short:
x = a & b + c * d && e ^ f == 7
is something nobody wants to readgoal:
a suggestion as to how to order the precedence is at the end of the post
from learning crystal so far I like strong typedefs and very reasonable auto-format which really helps the programmer and thus I think enforced clear operator precedence would fit in very well and keep code clean and safe
and it would definitely be better to post in a request for comment pool
The text was updated successfully, but these errors were encountered: