[Proposal] Allow custom per-property equality for records
#9953
Unanswered
sab39
asked this question in
Language Ideas
Replies: 1 comment 1 reply
-
|
Our position in the past is that this is too much customization for a language feature, and that this should be done as a source generator. Personally, my opinion hasn't changed. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Proposal: For a
recordtypeRwhich has a positional propertyPropof typeT, ifRalso has a static property or fieldPropEqualitywhose type implementsIEqualityComparer<T>, the generatedEqualsandGetHashCodemethods forRwill use that equality comparer forProp.In much the same way that Collection Expression Arguments were motivated by not wanting collection expressions to fall off an ergonomic cliff if a developer needs to pass a comparer to a dictionary, this would remove the current ergonomic cliff that exists if a developer wants to customize the equality behavior of a single property within a record.
Intentionally, this approach would be easy to build on using source generators to improve the ergonomics further, eg:
This proposal is inspired by Collections in records should be compared with value equality instead of reference equality, but isn't limited to collections and doesn't propose a breaking change to default behavior. With this proposal, you could get value equality of collections in records by using an
EqualityComparertype based onEnumerable.SequenceEqual(which I'm surprised doesn't seem to exist in the BCL today, but could be added, or is easy enough to write by hand).Beta Was this translation helpful? Give feedback.
All reactions