I miss the overloaded operators in Java, especially in the following situations:
A class that is an algorithm or functor: (Strategy template, chain of responsibility, translator, etc.). Overloading op () is natural; instead, each programmer comes up with (often incompatible and thus confusing) names for the functions: "eval", "rating", "operation", "doIt", etc. Thus, clarity is reduced, because these names we are forced to use do not make their meaning obvious.
A class that has a conversion to another type: In C ++, this Type () operator works both for the actual conversion, and for getting the internal member of the desired class. In the second case, a lot of things happen in Java when the class is unnecessarily final, but you want to add operations to it:
class DecoratedStringBuffer { //extends StringBuffer doesn't work, as String is final private String byContainmentThen; public decorate(final String prefix, final String suffix) { ... } public append(final String s) { byContainmentThen.append(s);} // other forwarding functions }
Since DecoratedStringBuffer is not -StringBuffer, before it leaves your code and returns to the client code, it needs to be converted back, presumably using a function that ultimately applies the suffix and prefix. It would be great if we could name this operator StringBuffer () (and even more if Java, for example C ++, can apply one user-defined transformation).
Instead, since there is no agreement, we must give it a name that is necessarily more ambiguous. getStringBuffer () is one obvious name, but for many Java users, which implies the corresponding setStringBuffer, which we do not need. Even if this does not mean that the name is ambiguous: is it the StringBuffer you are working with, or something else?
toStringBuffer () is the best name and template that I usually use, but then someone reading the code wonders why what looks like getter is called "in" ClassName.
Honestly, in addition to designing numerical classes or "explicitly" co-objects, it is little used for op + overloading. And since Java is not value-based like C ++, there is very little use for op =; Java does not try to make everything act as a primitive class of int values. These are op () and conversion operators, which I skip.