operator overloading outside the type
Walter Bright
newshound2 at digitalmars.com
Sat Mar 29 18:48:04 UTC 2025
On 3/29/2025 9:25 AM, Timon Gehr wrote:
> Yes. UFCS never overrides a matching member function, no matter how well the
> UFCS function matches. UFCS is not even attempted if a call can be resolved via
> member function.
That's correct. The issue brought up in my preceding post is *should* operators
be treated as if they were functions? Or are they special? I've seen what
happens when they are treated as functions. People write code that looks like
conventional C++ code, but with the operators overloaded to do something utterly
different. They'd write articles saying how cool this was. I had the opposite
reaction.
There are a number of deliberate characteristics in D that discourage writing
incomprehensible code, like no macros and no version algebra. The operator
overloading capability in D is deliberately restrictive, to make it harder to do
non-arithmetic things with it.
With functions, there are no implicit expectation of what is does, we all know
that. But the expectation with operators is `a+b` does an addition. (That's why
D uses ~ for concatenation, not +) If the user suspects that it does something
else, at least he can visit the type declaration to see if it was repurposed.
But overloading operators with freestanding functions? It becomes a nightmare
with non-trivial programs.
More information about the Digitalmars-d
mailing list