I thought by myself "why can't an equality comparer for records use the equals operator if the record has one?"

I thought by myself "why can't an equality comparer for records use the equals operator if the record has one?"

Well it can! ;)

https://bitbucket.org/snippets/sglienke/dLgbB

(prototype - work in progress)

Comments

  1. Jacek Laskowski For what? Using an equality comparer? Implementing a record where the RTL default comparer does not make sense? Or implementing an equals operator overload? For all that I guess you can find it using google.

    ReplyDelete
  2. If you know how TEqualityComparer.Default works for records you know the use case. If you don't, well then look into the code.

    "Which hash?" again - look into the code of Generics.Defaults. My code is just putting the responsibility for the default behavior into the type itself (just like TObject has Equals and GetHashCode method which the default comparer for classes uses btw) rather than using an in most cases useless and wrong CompareMem and building the hashcode from the record memory.

    FWIW as you might know the model of those comparers is taken from .NET and there also records can implement interfaces and the default equality comparer implementation checks for the IEquatable interface on a type when you call EqualityComparer. (see http://referencesource.microsoft.com/#mscorlib/system/collections/generic/equalitycomparer.cs,542680fa5b2d828d). And since everything is an object there it can just fall back to using those methods from object.

    ReplyDelete
  3. "I thought by myself "why can't an equality comparer for records use the equals operator if the record has one?""

    Don't ever get a job writing those little insights on the back of herbal tea boxes.

    ReplyDelete
  4. Joseph Mitzen You missed your calling ;)

    ReplyDelete

Post a Comment