Why Java Doesn't Need Operator Overloading (and Very Few Languages Do, Really)

Don nospam at nospam.com
Wed Apr 15 00:23:20 PDT 2009


Paul D. Anderson wrote:
> Sounds like someone needs a strong dose of D!!
> 
> http://java.dzone.com/articles/why-java-doesnt-need-operator
> 
> The comments bounce between "operator overloading is always bad because you can do idiotic things with it" and "operator overloading is essential because sometimes you really need it".
> 
> How about a language where operator overloading is available for the cases where you do really need it, but isn't available in general?
> 
> Hmmm...
> 
> Paul
He says he doesn't like it because:

The number of operators that you can overload is very small and each of 
them is attached to very specific semantics that makes little sense 
outside the realm of scalars and of a few other specialized mathematical 
concepts (e.g. matrices).

Bingo! That's what operator overloading is for. **Don't overload 
arithmetic operators unless you are doing arithmetic.** I think the main 
problem with operator overloading in C++ is that that point wasn't 
explained well at all. The D spec could probably do a better job of it, 
but at least the ~ operator removes the temptation for people to use + 
to mean concatenation.



More information about the Digitalmars-d mailing list