Skip to content
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

Closed
lu4p opened this issue Oct 20, 2020 · 9 comments
Closed

proposal: Go 2: allow combining && of comparisons #42095

lu4p opened this issue Oct 20, 2020 · 9 comments

Comments

@lu4p
Copy link

@lu4p lu4p commented Oct 20, 2020

I find it tedious to write the same condition multiple times.
I suggest adding an abbreviated form, to improve readabilty and reduce stutter.

Example:

// current form
if i <= 0 && j <= 0 && k <= 0 {
	return
}

// proposed equivalent
if [i, j, k] <= 0 {
	return
}

Suggested EBNF:
Current spec: https://golang.org/ref/spec#Expression

Expression = UnaryExpr | (Expression binary_op | "[" ExpressionList "]" rel_op)  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.

@gopherbot gopherbot added this to the Proposal milestone Oct 20, 2020
@gopherbot gopherbot added the Proposal label Oct 20, 2020
@mvdan
Copy link
Member

@mvdan mvdan commented Oct 20, 2020

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.

@DeedleFake
Copy link

@DeedleFake DeedleFake commented Oct 20, 2020

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.

@lu4p
Copy link
Author

@lu4p lu4p commented Oct 20, 2020

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.

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
}
@ianlancetaylor ianlancetaylor changed the title proposal: allow to simplify multiple Expressions to one Expression proposal: Go 2: allow combining && of comparisons Oct 20, 2020
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Oct 20, 2020

See also #33694 for another proposed change that combines comparisons.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Oct 20, 2020

In Go as in some other languages the && operator short-circuits: if the left expression is false, the right expression is not evaluated. With this syntax, what happens with (f1(), f2(), f3()) > 0?

@lu4p
Copy link
Author

@lu4p lu4p commented Oct 20, 2020

In Go as in some other languages the && operator short-circuits: if the left expression is false, the right expression is not evaluated. With this syntax, what happens with (f1(), f2(), f3()) > 0?

It would be evaluted as f1() > 0 && f2() > 0 && f3 > 0, so it also short circuits.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Oct 20, 2020

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.

@robpike
Copy link
Contributor

@robpike robpike commented Oct 21, 2020

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.

@lu4p
Copy link
Author

@lu4p lu4p commented Oct 21, 2020

I'm closing this for now as this design doesn't seem to get much support.

@lu4p lu4p closed this Oct 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.