Replies: 3 comments 5 replies
-
OK, maybe I'm being unkind with the above, but there's a lot of truth in it. The point is, history has repeated shown us that the one effect you will not achieve with this is to "get rid of "Equality Comparers" and other Equals boilerplate", for many people will stick with old ways of doing things. So this will just become yet another way of doing equality that one must learn. Plus I don't really see what this proposal offers that records don't offer with even less boilerplate. But maybe I missed something? |
Beta Was this translation helpful? Give feedback.
-
Sounds like a good use case for a source generator. |
Beta Was this translation helpful? Give feedback.
-
In defense of what I am proposing: I hate having to write a bunch of Equality Comparers and HashCodes. Boring, error prone, not DRY, wastes development time, you should write a test for the equality comparers if you are doing TDD, etc. If I add a new column or change existing distinct columns, I have to change both the Equality Comparer and HashCode, The only real code of significance is what columns are we want to be distinct. As for source generator suggestion, why do that? You as the developer would have to tell VS what are the distinct columns anyway and then with the longer generated code - why would you want more code to maintain? @DavidArno, As for the "for many people will stick with old ways of doing things" attitude, why even read and comment and accept any new features? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Discussion: New ability to describe what makes a class distinct
Purpose: So one can get rid of "Equality Comparers" and other Equals boilerplate for the majority of classes.
Core Benefits;
Why?: "Equality Comparers" and HashCodes are: 1) Boring, 2) error prone, 3) not DRY, 4) wastes development time, 5) you should write a test for the equality comparers if you are doing TDD. 6) Brittle - If I add a new column or change existing distinct columns, I have to change both the Equality Comparer and HashCode, The only real code of significance is what columns are we want to be distinct.
New Keyword in Context of the Class: DISTINCT (its usage shown below)
Class Example:
In the example given:
ExpectedRow and ExpectedCol do not matter comparing the equality of these objects, but the others do. Example given is from real world. We have app that reads from a YAML configuration that users could change. We read the YAML into an class. We wanted to prevent duplicate rows because users could have entered a row twice. So wanted to be able to determine row distinctness so we don't want duplicates.
New Compiler Restrictions:
1) The compiler should error if the properties in the Distinct are not properties in that class. So in the example "SetName" and all the others must be a property on the AnchorCellConfig class.
2) The compiler should error if object has another EqualityComparer on it.
New Compiler Actions:
It could construct a EqualityComparer for you behind the scenes. This is now the new default EqualityComparer of the object.
Existing C# language impacts:
Equality boilerplate no longer needed:
NOTE - This is the shortest version of an EqualityComparer I can think of:
Clarification: The DISTINCT keyword and line are not required. This does not remove old EqualityComparers and Hashcodes from existing objects. Nor does it prevent using EqualityComparers and Hashcodes for specialized comparisons.
Beta Was this translation helpful? Give feedback.
All reactions