Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BCL Immutable Collections: equality is non-symmetric

Since immutable data strucutures are first-class values we can compare them for equality or order as we do with any other values. But things became complicated in BCL immutable collections preview because every immutable collection can be parameterized by IEqualityComparer<T>/IComparer<T> instances. Looks like immutable collections with different comparers should not be allowed to compare (since equality is not defined for comparers itself), because it makes equality relation non-symmetric:

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 behavior be fixed somehow?

like image 463
controlflow Avatar asked Oct 22 '22 17:10

controlflow


1 Answers

Equality is a difficult thing to define, and even more difficult to achieve consensus on that definition in a room of smart engineers. :) We're actually going to be removing value-equality from the Equals and GetHashCode methods of the immutable collections so that these methods provide the same speed (and near uselessness) as most other types in the BCL and in customer code.

We do very much hope to add value-equality methods to the immutable collections in a future release.

like image 198
Andrew Arnott Avatar answered Oct 26 '22 22:10

Andrew Arnott