operator overloading outside the type

Dukc ajieskola at gmail.com
Fri Mar 28 09:49:29 UTC 2025


On Friday, 28 March 2025 at 08:57:39 UTC, Jonathan M Davis wrote:
> Imagine the weird side effects that you'd get and hard to track 
> down bugs if something like opCast were overloaded externally.

I don't see how it would be any more of a problem than the 
possibility to do something like

```D
// MWHAHAHAA!!
@trusted dup(T)(T[] arr) => cast() arr;
```

that already is here. Besides, we could prevent overloading 
already existing internal operators like integer addition to do 
anything different without any special rules. The overloads of 
the member functions are tried first, module-level operator 
overloads being used only if no member overloads are found. If 
we'd treat the langauge operators as member functions of the 
primitive types, you couldn't override them.

However, I'm not necessarily against treating them as overridable 
module-level operators instead since it still wouldn't be any 
different from my code example here.




More information about the Digitalmars-d mailing list