Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I have to overload operators when implementing CompareTo?

Tags:

c#

icomparable

Let's say I have a type that implements IComparable.

I would have thought it's reasonable to expect that the operators ==, !=, >, <, >= and <= would "just work" automatically by calling CompareTo but instead I have to override them all if I want to use them.

From the language design perspective is there a good reason it was done this way? Are there any cases when you it's genuinely useful for A>B to behave differently to Compare(A,B)>0?

like image 918
Andy Avatar asked Dec 15 '13 11:12

Andy


People also ask

Why would you need to overload a comparison operator?

Operator overloading is a crucial concept in C++ that lets you achieve the functionality of the built-in operators while working with user-defined data types. Comparison operators in C++ are the ones that are there to compare two values with each other such as “==”, “!=

When implementing IComparable t you should also override Equals?

When you provide an implementation of IComparable, you must usually also override Equals so that it returns values that are consistent with CompareTo. If you override Equals and are coding in a language that supports operator overloads, you should also provide operators that are consistent with Equals.

How do you overload an operator?

Operator Overloading in Binary Operators Here, + is a binary operator that works on the operands num and 9 . When we overload the binary operator for user-defined types by using the code: obj3 = obj1 + obj2; The operator function is called using the obj1 object and obj2 is passed as an argument to the function.

How do you define operator overloading in C++?

An overloaded operator is called an operator function. You declare an operator function with the keyword operator preceding the operator. Overloaded operators are distinct from overloaded functions, but like overloaded functions, they are distinguished by the number and types of operands used with the operator.


2 Answers

The whole situation is vexing. C# has too many ways to express equality and inequality:

  • the == != > < >= <= operators (which are logically static methods)
  • the Equals static method (which calls the virtual method), the Equals virtual method, the ReferenceEquals method
  • The IComparable and IEquatable interfaces

They all have subtly different semantics and with the exception of static Equals, none automatically uses the other, and none actually has the behavior that I want. Static methods are dispatched based on the compile-time type of both operands; the virtual methods / interface methods are dispatched based on the run-time type of one of the operands, which makes the operation asymmetric; the type of one side matters more than the type of the other.

I can't imagine that anyone thinks that the situation we're in is great; given no constraints, this is not what would have evolved. But managed language designers do have constraints: the CLR does not implement static methods in interface contracts or double-virtual dispatch, or the ability to put an operator constraint on a generic type parameter. And therefore multiple solutions have evolved to solve the equality/inequality problem.

I think that were the CLR and C# designers to go back in time and tell their past selves what features ought to be in v1 of the CLR, some form of static methods in interfaces would be high on the list. If there were static methods in interface then we can define:

interface IComparable<in T, in U>  {     static bool operator <(T t, U u);     static bool operator >(T t, U u);     ... etc 

And then if you have:

static void Sort<T>(T[] array) where T : IComparable<T, T> 

You could then use the < and == and so on operators to compare elements.

like image 107
Eric Lippert Avatar answered Oct 24 '22 02:10

Eric Lippert


Two main reasons:

  1. It is a general structure for all operators. While the comparison operators may never have alternative semantics, there is great utility in a structure that allows very different semantics for some of the other operators. Implementing a separate structure for just the comparison operators would have required omitting some other, probably much more useful, feature. Check out this elegant implementation of BNF within C# for an example.
  2. The default implementations, for the case of value types that have it, relies of necessity on Reflection, and thus is horribly inefficient. Only you actually know the most efficient way to implement these operators for your classes. In many cases not all fields of a struct need to be compared to test equality, nor do all fields always need to be combined in a suitable GetHashCode implementation. No default implementation can ever determine that for all types, because it is reducible to the Halting Problem.

Update as per Eric Lippert among others, the following is the appropriate standard implementation of the comparison operators in C# for a type UDT:

public int  CompareTo(UDT x) { return CompareTo(this, x); } public bool Equals(UDT x)    { return CompareTo(this, x) == 0; } public static bool operator  < (UDT x, UDT y) { return CompareTo(x, y)  < 0; } public static bool operator  > (UDT x, UDT y) { return CompareTo(x, y)  > 0; } public static bool operator <= (UDT x, UDT y) { return CompareTo(x, y) <= 0; } public static bool operator >= (UDT x, UDT y) { return CompareTo(x, y) >= 0; } public static bool operator == (UDT x, UDT y) { return CompareTo(x, y) == 0; } public static bool operator != (UDT x, UDT y) { return CompareTo(x, y) != 0; } public override bool Equals(object obj) {     return (obj is UDT) && (CompareTo(this, (UDT)obj) == 0); } 

Just add the custom definition for private static int CompareTo(UDT x, UDT y) and stir.

like image 43
Pieter Geerkens Avatar answered Oct 24 '22 02:10

Pieter Geerkens