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

Simen Kjaeraas simen.kjaras at gmail.com
Tue Apr 14 17:47:52 PDT 2009


On Wed, 15 Apr 2009 02:29:33 +0200, Rainer Deyke <rainerd at eldwood.com> wrote:

> Walter Bright wrote:
>> My problem with operator overloading stemmed from C++'s endorsement of
>> using >> and << for stream operations.
>
> I don't see how "stream << value" is any worse than "shift_left(stream,
> value)".
>
> Most operators are just functions with special syntax.  No more, no
> less.  The issues with operator overloading are exactly the same as the
> issues with function overloading.

The problem with operator overloading as compared to function overloading
is that they are generally shorter, and thus some programmers want to use
them not because they make logical sense but because they lead to less
typing. Also, some operators (<<, for instance) may seem to make sense in
situations where many would think they do not.

shift_left(stream, value) would simply not be used to output 'value' to
'stream', while stream << value is provably being used like that, even
though the meaning I can glean from the two is equal. I fear C++'s '<<'
was a prime example of 'look what I can do'.

I read a nice text about operator overloading some time ago, that likened
it to function naming. Basically, the argument that operator overloading
can be abused and thus no programmer should be trusted to use it is no
different from saying function naming can be abused and thus no programmer
should be allowed to choose the names of functions.

--
Simen



More information about the Digitalmars-d mailing list