Am I evil for this?

Atila Neves atila.neves at gmail.com
Mon Oct 24 11:25:58 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.

It depends. I don't think this is unexpected:


struct Path {
     string value;
     Path opBinary(string op)(in string path) if(op == "/") {
         import std.path : buildPath;
         return Path(buildPath(value, path));
     }
}

void main() {
     assert(Path("/foo") / "bar" == Path("/foo/bar"));
}



More information about the Digitalmars-d mailing list