Am I evil for this?
Max H
maxhaton at gmail.com
Fri Oct 14 21:07:52 UTC 2022
On Friday, 14 October 2022 at 18:29:10 UTC, Walter Bright wrote:
> On 10/14/2022 12:54 AM, Atila Neves wrote:
>> As Bjarne said once in response to complaints that operator
>> overloading lets people write code that doesn't do what you
>> expect:
>>
>> ```
>> // notice how the code and the docs lie
>> /**
>> * Adds two numbers
>> */
>> int sum(int i, int j) {
>> return i - j; // oops
>> }
>> ```
>
> True that documentation often (always?) lies about what the
> code actually does. But the causes of this are usually because
> of programmer laziness and/or error in documenting it correctly.
>
> But operating overloading for non-arithmetic purposes is
> *deliberately* doing the unexpected.
A slightly more useful way of thinking about this I think is to
think about it algebraically, i.e. If the nu-operation is
confusing algebraically then it is *definitely* wrong versus
merely not to taste. You can do useful algebra on things that
wouldn't typically be considered arithmetic, that algebra often
has an analogue in familiar arithmetic on numbers (we'll gloss
over floating point arithmetic...)
In the case of `sum`, we'd expect it to be associative - it's
not, something has gone wrong. This is a more precise way of
expressing that "Go home, you're drunk" feeling from this kind of
code.
D not using + for strings was something that helped sell me on it
early on. Intuitively we all know concatenation isn't commutative
anyway, but it doesn't hurt to express it lexically.
More information about the Digitalmars-d
mailing list