StephenCleary / Comparers

The last comparison library you'll ever need!
MIT License
427 stars 33 forks source link

Better syntax for wrapping other comparers #14

Closed StephenCleary closed 5 years ago

StephenCleary commented 5 years ago

It is sometimes desirable to wrap other comparers with a Nito.Comparers comparer that just forwards to the underlying comparer. This brings the benefit of not throwing when GetHashCode(null) is called (see #13). We could make this wrapping easier.

Counterpoint: this wrapping makes it easy to treat an IComparer<T> as though it were an IEqualityComparer<T> (since the wrapper is an IFullComparer<T>). Currently, this would cause a runtime error if GetHashCode is used without being implemented (see https://github.com/StephenCleary/Comparers/wiki/Pitfalls). By making the wrapping easier, this pitfall is more exposed. However, it probably will remain a rare occurrence.

Possible syntax options:

StephenCleary commented 5 years ago

v6 will have a WithStandardNullHandling extension method in the Nito.Comparers.Fixes namespace.

https://github.com/StephenCleary/Comparers/blob/f706547c923d296e9f1f0651c0940c68f107a1de/test/UnitTests/FixComparers.cs#L12-L23