
-----------------------------------
DtY
Thu Dec 23, 2010 12:29 pm

How much operator overloading should be done?
-----------------------------------
There's a lot of debate on whether or not operator overloading should be available in languages, but I think it would be more meaningful to establish how much operator overloading should be done.

The case against operator overloading is that operators are often overloaded to do something that that operator is not meant to mean. For example, what does it mean to add two strings? This might seem obvious to anyone who has used a language that supports operator overloading (and for some reason Java Strings are allowed to), but in reality concatenating strings is very different than arithmetic addition.

But does that mean that mean we should not be allowed to overload the addition operator? I say no, because there are cases that you would want to use the addition operator on your own class, in a case that it would be a lot like arithmetic addition, in the case of arbitrary precision integer classes, rational number class, complex number class; in all of these addition does mean addition.

And then there's the equality operator. You will likely want to test if two objects of any class are equal (and how often will you want to check if they point to the same place?). It's not a stretch to say that "hello" is equal to "hello" but is not equal to "world", where it would be to say that "hello"+"world" is "helloworld". But then, we get into more problems with the inequality operators (not equal to is obvious, of course). Is "hello" more than "world"? Most languages with operator overloading will compare the characters' ordinal values one by one until it finds one that's not equal. And how useful is this really? it's not even guaranteed to be the same across platforms.

I'm actually okay with overloading the arithmetic operators, I think it's pretty natural that adding two strings is the same as concatenating them, but this may be because my first language is python. I can't say what I'd think if my first language was Perl or PHP, which have a different string concatenation operator. So maybe we can decide that + is actually the addition and concatenation operator, and then we can use it on all sorts of objects.

And then we have unordered types such as sets and hashmaps (dictionaries). What does it mean to add two sets together? Maybe this is just weird because in maths we made up a new operator for the union and intersection operators. What we want to do is find all the objects that are in a OR in b, this sounds like the or operator. Want everything that is in a AND in b? That's the and operator. But is this really natural? The boolean and and or operators act a lot differently than the set union and intersection operators. Are these really natural? It might just seem natural to me because I've never used a language that has its own set union and intersection operators.

But now, these operators are not the reason I made this topic, I think all the aforementioned operators are natural enough that they should be used like that. But, there are a few more I'm not so sure about.

The biggest culprit here is the bit shift operators (>). C++ famously overloaded these so that they were now the bit shift or stream extraction/insertion operators. Wait what? Those aren't even similar. So why is 