BCL Immutable Collections: Asymmetric Equality

Since immutable data structures are first-class values, we can compare them for equality or order, as for any other values. However, in previewing BCL collections, things got complicated because each immutable collection can be parameterized by instances of IEqualityComparer<T> / IComparer<T> . It seems that immutable collections with different mappers should not be mapped (since equality is not defined for the mappings themselves), since it makes the equality relation asymmetric:

 var xs = ImmutableList<string>.Empty.Add("AAA") .WithComparer(StringComparer.OrdinalIgnoreCase); var ys = ImmutableList<string>.Empty.Add("aaa") .WithComparer(StringComparer.Ordinal); Console.WriteLine(xs.Equals(ys)); // true Console.WriteLine(ys.Equals(xs)); // false 

Will this fix be fixed in any way?

+4
source share
1 answer

Equality is difficult to define, and it is even more difficult to reach a consensus on this definition in the room of smart engineers. :) In fact, we are going to exclude the equality of values ​​from the Equals and GetHashCode methods of immutable collections, so that these methods provide the same speed (and almost uselessness) as most other types are in BCL and in client code.

We really hope to add equity in immutable collections in a future release.

+3
source

All Articles