runtime: ValueTuple throws for null comparer, unlike other BCL APIs
Continuing from https://github.com/dotnet/corefx/issues/18432.
You would expect this to succeed, but it throws NullReferenceException:
((IStructuralEquatable)(1, 2)).Equals((1, 2), null)
I’ll quite often take advantage of the fact that all BCL APIs use EqualityComparer<T>.Default when you pass null, and chain constructors and other methods with the parameter IEqualityComparer<T> comparer = null. If my own constructor or extension method takes IEqualityComparer<T> comparer = null, I assume that I can pass that into the BCL method. It’s not intuitive to make it the call site’s responsibility to check for null and pass EqualityComparer<object>.Default or call one or the other BCL overload depending whether comparer = null.
It’s not critical since the workaround is to pass comparer ?? EqualityComparer<object>.Default instead of comparer. This is an API gotcha that may go unnoticed until code is in the field though.
ValueTuple<*>.IStructuralEquatable.Equals and ValueTuple<*>.IStructuralComparable.CompareTo have no null comparer check. If it followed the convention set by all other BCL methods, it would use EqualityComparer<object>.Default if you pass null.
Array.IStructuralEquatable.Equals and Array.IStructuralComparable.CompareTo, and Tuple<*>.IStructuralEquatable.Equals and Tuple<*>.IStructuralComparable.CompareTo have the same problem. They are in coreclr. Should I open an issue over there?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 29 (29 by maintainers)
The conclusion is that the benefits do not outweigh the costs. See https://github.com/dotnet/coreclr/pull/11345#issuecomment-300616611
Thanks for the discussion and I look forward to the next one! =)
@jnm2 I’m not sure either (between
Comparer.DefaultorComparer<object>.Default. I’ll take a deeper look tonight. The PR reviewers may also be able to chime in.Just to keep you updated, the compat council is still discussing the question. I’ll let you know once settles (hopefully with an approval).