-
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
Allow chaining of comparisons #2083
Comments
It's sounds problematic since |
Huh. It does. I had to check for myself, since I couldn't believe anyone could have had such a terrible idea. However, such chaining of operators is still possible to implement, since it's currently rejected as an error. I'm not saying I want it to be implemented, I'm actually against the idea, but it's not a technical problem. |
At least for LT, GT, LTE, GTE boolean cannot be a type on a side of one of the operators. |
Actually |
The best use case I've seen for this is a range bound, like What other use cases would fit this, that wouldn't be more clearly written some other way? |
I've no strong feelings on this since the compiler already prevents chained comparison operators. I'm curious though : What languages do this currently? |
Python that I know of |
@joshtriplett it's more useful for generic code than anything (also, every language I know of has the relational operators for |
In Python Don't know enough to judge from the rustc implementation's perspective, though. |
I feel like this could be an optional thing, enabled by some #[pragma]. I mean, we already special-case the parse, just add a few hooks to make it like the Python implementation. OTOH, there's also semantics to consider. Like, does this syntax short-circuit? If I write |
If it was to work like Python then in the first case the function would not run since the operators are lazy and I don't see why not allowing chaining of multiple compares since it would be like concatenating multiple comparisons with short circuit and |
I meant more in the sense of "this adds unnecessary complexity to our language specification for a feature that's pretty much optional" But looking back, there's really no reason not to do it, certainly not if it's a |
@cramertj yeah lol, sorry. Anyways, I like this idea, it's quite nice and follows, at least my, intuition. |
cc #561. |
Just to add that Julia also allows this https://docs.julialang.org/en/v1/manual/mathematical-operations/#Chaining-comparisons-1 |
In real world no one writes |
Namely for all comparison operators they should be allowed to be chained. The easy way of disambiguating them is since they are all binary operators just chain them with
&&
. Lastly, this can help with code clearness since the following can be avoidedand replaced with:
where the return values of any non_constant is held in reserve so that the function isn't called multiple times.
The text was updated successfully, but these errors were encountered: