[Proposal]: Use CompareTo to resolve missing comparison operators #4119
Replies: 8 comments
-
Since this isn't a fully-specified language proposal, it should've been opened as a discussion. Personally I'm not a fan, and I don't think this could be introduced without it being a breaking change. It seems like a good candidate for a source generator to me. |
Beta Was this translation helpful? Give feedback.
-
How? The code doesn't compile today, so it can't break anything. |
Beta Was this translation helpful? Give feedback.
-
It does for the equality operator. |
Beta Was this translation helpful? Give feedback.
-
No, it doesn't (.net core 3.1). |
Beta Was this translation helpful? Give feedback.
-
Yes it does. You'd be changing the meaning of See this example where |
Beta Was this translation helpful? Give feedback.
-
@PathogenDavid |
Beta Was this translation helpful? Give feedback.
-
Ah, I missed the part where you were implementing I feel like if you're going to go that route it's just going to be this weird inconsistency where this only works when comparing reference types and value types or value types with other value types. In that issue you've mentioned you're auto-generating these types. Why not just auto-generate your operator overloads too? |
Beta Was this translation helpful? Give feedback.
-
Recently. this seems to be the solution. But I wonder why can't the compiler deduce most of the operators if it has just enough info? In fact this doesn't apply only on CompareTo, but it is applicable also if the compiler has one of ( |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Given this class that implements the IComparable:
Why can't c# use to the CompareTo method to resolve the comparison operators?
which can be loqered to:
var f = new Test() { Value = 1};
Console.WriteLine(f.CompareTo(1) == 0);
Console.WriteLine(f.CompareTo(1) != 0);
Console.WriteLine(f.CompareTo(1) == 1);
Console.WriteLine(f.CompareTo(1) != -1);
Console.WriteLine(f.CompareTo(1) == -1);
Console.WriteLine(f.CompareTo(1) != 1);
Beta Was this translation helpful? Give feedback.
All reactions