Replies: 11 comments 4 replies
-
Forgive me if I am wrong, but isn't |
Beta Was this translation helpful? Give feedback.
-
IMO this is too much work for too little benefit. How would you parse |
Beta Was this translation helpful? Give feedback.
-
In
As with any other instance of implicit casting, the most specific variant will be used. |
Beta Was this translation helpful? Give feedback.
-
@Tyrrrz this means that to preserve backward compatibility, chaining equality cannot be implemented for bool canRead1 = CheckPermissions(user1, file);
bool canRead2 = CheckPermissions(user2, file);
if (canRead1 == canRead2 == Settings.PermissionsShouldMatch) { ... } Implementing a syntactic form for all types that allow Chaining inequality is a good proposal, though. |
Beta Was this translation helpful? Give feedback.
-
Probably some parenthesis required to enable math-like expression: if ([[ a < b <c ]]) { ... } And other operators should not be allowed in this expression, otherwise if ([[ a< b && b > c ]]) even if good will always too "brain bending" and error prone. Anyway this feature have too little benefit for required efforts. |
Beta Was this translation helpful? Give feedback.
-
@apskim: We can overload operators... "==" does not need to return a boolean, we can also return other data types, e.g. three-state-booleans, as long as both "==" and "!=" return the same type. I think this also applies to inequality. Especially when using multiple state logic ("false", "maybe", "true"), those three comparisons may return different results, and all be syntactically valid:
|
Beta Was this translation helpful? Give feedback.
-
https://gist.github.com/ufcpp/3509ba373ab1622ac9b77e78dea26ecc |
Beta Was this translation helpful? Give feedback.
-
Seems to be rather a case for a NuGet package or .NET framework proposal. You cannot expect to replicate @ufcpp 's fine code every time you need it in one place 😉 |
Beta Was this translation helpful? Give feedback.
-
The problem in OP is certainly valid, but is there any proposed language feature that can actually make the situation better, without breaking existing projects, and without more complicated syntax than just writing out all of the comparisons combined with Are there other languages that can handle chaining comparison operators like this? |
Beta Was this translation helpful? Give feedback.
-
With if (a is >= 5 and <= 10) |
Beta Was this translation helpful? Give feedback.
-
I am also frequently annoyed by the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Motivation
In math, ranges are very often specified by chaining multiple inequality operators, like so:
5 <= x < 9
or0 < a <= b < 3
When working with code that deals with numbers, you often end up having to either check equality of multiple variables or check whether they're contained in a certain range. Normally you just write long conditional statements like these:
if (a == b && b == c && c == d) { }
if (a >= 5 && a <= 10) { }
Of course, to improve readability, you can write your own helper methods:
if (AllEqual(a, b, c, d)) { }
if (a.InRange(5, 10)) { }
Proposal
Allow chaining comparison operators:
if (a == b == c == d) { }
(instead ofif (a == b && b == c && c == d) { }
)if (5 <= a <= 10) { }
(instead ofif (a >= 5 && a <= 10) { }
)Beta Was this translation helpful? Give feedback.
All reactions