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
proposal: Go 2: allow combining && of comparisons #42095
Comments
Go can be a bit verbose, but that's often for the sake of keeping the language simpler to learn and understand. I worry that this change would save you a few keystrokes in some very specific cases, but at the cost of making the code significantly less readable - especially because there's no precedent in other languages with this syntax. |
This seems like an extremely specific feature. It is only useful if you have several variables that you want to compare to the same other thing and AND all the comparisons together. You can't OR them, you can't check them against different values, and if you ever decide that you need something more complicated, you'll have to rewrite the entire thing. I agree that it simplifies that specific case a bit, but it doesn't seem particularly orthogonal. |
You can use AND instead of OR, example: OR: if i <= 0 || j <= 0 || k <= 0 {
return
} Equivalent AND: if !(i > 0 && j > 0 && k > 0) {
return
} Proposed equivalent: if !([i, j, k] > 0) {
return
} |
See also #33694 for another proposed change that combines comparisons. |
In Go as in some other languages the |
It would be evaluted as |
Thanks. I don't know how else this could be implemented, but it seems potentially surprising that in a list of function calls only some of the calls are executed. |
I find none of the rewrites clearer than the original. The syntax is shorter but much less clear. There's no intuition, for instance, about whether (i, j) < 0 means i < 0&& j < 0 or i < 0 || j < 0. It gets harder to decode as the expressions grow. |
I'm closing this for now as this design doesn't seem to get much support. |
I find it tedious to write the same condition multiple times.
I suggest adding an abbreviated form, to improve readabilty and reduce stutter.
Example:
Suggested EBNF:
Current spec: https://golang.org/ref/spec#Expression
Only a
rel_op
would be allowed after an"[" ExpressionList "]"
, because the other operators don't really make sense with multiple values.Backwards compatibility:
This change is backwards compatible, because such Expressions are currently rejected by the compiler.
Other languages:
I currently don't know that a programming language has a similar concept, but if anyone knows a similar or maybe better concept from another language, please let me know.
The text was updated successfully, but these errors were encountered: