-
Notifications
You must be signed in to change notification settings - Fork 4k
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] Better handling of multiple comparisons with the same variable (a > min && a < max
)
#136
Comments
It was mentioned on CodePlex that functionality like this could be pretty easily accomplished through the pattern matching proposals and extension methods. e.g.: using static Some.Namespace.RangePatternMatcher;
if (x is between(5, 10)) { } // range comparison
if (x is any(1, 2, 3, 4, 10)) { } // set comparison |
NuGet: Exts.Ranging.Classes.Operators if the type of the values are all ints, some coders think it should compile since you can't do |
Yes, both ranges and I like the use of |
I've pondered a similar feature before. Here's my proposal, which I'm going to repost partially from my blog. The idea is to introduce "operators"
which would translate into this:
Or, to improve flexibility and readability we could require the operator to be specified separately for each value, like so:
This also enables concise range comparisons, for example:
and is nicely generalized, unlike, for example, the SQL Full post where I discuss this in more detail. One downside is that this could be confusing for those who are used to "and" and "or" being the actual && and || operators, like Python. |
What about |
@alrz Is your proposal equivalent to "Solution A" above? |
This can be done with a parameterized active pattern (#5718) or overloaded
And for equality test you can use disjunctive-pattern (#6235)
|
The syntax The problem with a syntax such as You can at least avoid the reevaluation in C# 7 using patterns, for example, It isn't clear there is a palatable direct solution to this problem. It doesn't arise often in code, so whatever solution we came up with would really only be a marginal improvement to the language. We don't see a solution we feel we can take. |
@gafter A range expression e.g. |
@alrz The |
Problem
Consider these very common code patterns:
I think it is worth considering how verbosity here can be reduced -- especially since it could also give a performance benefit for cases like
Potential solutions
A. Ternary comparisons
For
<
,>
,>=
, and<=
one solution can be Python-likemin < a < max
.Pros:
a
can be evaluated exactly onceCons:
a == x || a == y
.B. Range comparisons
Example:
Pros:
switch
(case 1..3:
).Cons:
C. Quantum superpositions
Example:
This is similar to Perl's Quantum Superpositions and something that was already implemented in Microsoft Research's Cω.
Pros:
switch
Cons:
x > any(1,2)
is possible, but useless in most scenarios). Could be combined with solution A for a limited comparison support.The text was updated successfully, but these errors were encountered: