Allow deep equality for collections in records #5767
Replies: 5 comments 18 replies
-
This is a great suggestion, especially your option 1 - although it probably belongs in runtime repo. In the meantime, you should be able to make your own collection types? I don't recall, are things like |
Beta Was this translation helpful? Give feedback.
-
Once at this point, you basically have a source generator. So the guidance would be: you can do this today, without any need for language change. Just package up these specialized equality constructs into their own analyzer/generator lib and you'll be good to go. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Here's the approach I took:
|
Beta Was this translation helpful? Give feedback.
-
Duplicated #4845 |
Beta Was this translation helpful? Give feedback.
-
Records are a great addition to the language and have the potential to save developers some time and avoid bugs by not having to write boilerplate code for things like equality. However, the behavior of the
Equals
method on collection classes doesn't mesh well with what many developers would want for their records, which is deep equality. Right now, if you want deep equality, you have to manually implementEquals
and callSequenceEqual
from LINQ on the collections in the record, which wastes the developer's time and has the potential to lead to bugs if they add a field and forget to add it to their implementation ofEquals
. I understand that the behavior on existing collection classes can't be changed due to the need to maintain backward compatibility. However, I see two ways that this issue could be addressed:RecordList
orDeepEqualList
that overrideEquals
to do deep equality. Similarly forDeepEqualDictionary
,DeepEqualSet
, etc.[DeepEqual]
or[SequenceEqual]
that can be applied to a field in a record (or even an entire record) to signal the compiler to generate code to callSequenceEqual
on that field (or all fields of types implementingIEnumerable
) in the implementation ofEquals
for the record.Beta Was this translation helpful? Give feedback.
All reactions