Definition
First you need to define abuse in this context.
To me this means re-defining the operators so that they do not work intuitively for anybody reading the code.
But of course intuitively is also dependent on the context, but not only the context but the classes that are involved. So it boils down too will the use of operator overloading make the code harder to read or lead to any misunderstanding
by somebody naively reading the code without understanding the full context.
Matrix x;
Matrix y;
Matrix z = x * y; // Is this abuse.
That depends. Is your primary audience mathematicians (or smart CS students). If yes then the this is not abuse but tidy short hand. If your primary audience is an English Literature professor then maybe you should have written it like this:
WordUsage Hamlet;
WordUsage Othello;
WordUsage Shakespeare = Hamlet.DeduceLoveCorrelation(Othello);
// OK that was not clear to me but I am not an Literature buff.
// But to a literature person those identifiers may mean something.
Usage
So in C++ you can redefine the operators to do anything (C++ gives you a lot of flexibility (but this is also how the stream operators were introduced)). So abuse can turn into common usage if the idiom is accepted by the community as useful shortcut.
Does this help to deter programmers from abusing overloading in the aforementioned manner?
Does making it more convoluted to define operator overloading deter abuse. No. Its not as if using __add __
is any less hard than using operator+()
.
Anyone have any experiences of Python operator overloading being abused?
I think the python community has learned from the abuse and subsequent press about abuse that C++ had over a decade ago. When C++ came out it was the first mainstream language that allowed overloading of operators and people went hog wild experiment with the concept. This lead to a lot of frustration from maintainers and articles about overloading operators is an abuse (which led to a backlash and languages like Java banning them for no good reason (other than the author was swayed by public opinion).
Note: A couple of less popular language did it first (operator overloading) but they were mainly used by sensible people in research establishments. So either they were smart enough not to abuse the operators, or there research was so narrow that few bothered to read their code and thus it got no press.
__add__
oroperator+()
, but that doesn't stop the "abuse."+
for sequencing parsers and concatenating the results with+
)?