I don't see how "c1 = c1 + c2" is that much easier than "c1.addAll(c2)". Another issue is what the type of "c1 + c2" should be. The type of c1? The type of c2? Something else? Or would you only want to allow operators when combined with assignment, like "c1 += c2"? In that case the bulk operators are more than adequate.
The only classes for which I think mathematical operators should be overloaded are BigInteger and BigDecimal. Both are immutable classes that represent mathematical types. I really hope that the rumours of Sun adding them in Java 7 are true. Unlike my example of collection appending, "b1 * b2" is a lot better than "b2.multiply(b2)" because it's more natural.
Bear Bibeault wrote:I wrote C++ for years, and I don't miss it one iota.
I still code in C++ ... well, 20% of my time anyway.
Back in the days, when I just started coding in C++ (and at 100% of the time), my pet peeve was the cout syntax. How is shifting left to mean writing to a file intuitive? It was just silly to me. Add to that, all the operator overloading that was done because someone thought it was cool. Sheesh.
Today, luckily, I think most programmers learned the lesson and only use operator overloading sparingly. Even so, when I encounter a class, that I didn't write, for the first time, I always go through the definition in the include file to check for any operator overloading.
Jesper Young wrote:In my opinion it would have been good if Java had operator overloading. That would have made the wrapper classes (Integer, Long, etc.) and classes like BigInteger and BigDecimal easier to use.
Fortunately, with auto(un)boxing, you get to use mathematical operators on all wrapper classes. That only leaves BigInteger and BigDecimal, and as I've said, there are rumours that they will get special treatment by the compiler just like String has.
I'm not impressed by special-case compiler hacks when it should be handled cleanly by the language. And I shouldn't have to hack a compiler to get the same functionality for Complex, Matrix, or whatever else I want to do, either.
subject: Why didn't java support operator overloading