Martin Nowak is officially MIA
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Wed Mar 19 10:35:45 UTC 2025
On Wednesday, March 19, 2025 12:05:24 AM MDT Manu via Digitalmars-d wrote:
> Why didn't his operator overloading experiment work?
>
> struct S {
> int x;
> }
>
> // non-member opBinary
> S opBinary(string s : "+")(S lh, S rh) {
> return S(lh.x + rh.x);
> }
>
> void main()
> {
> S a, b;
> S s1 = a.opBinary!"+"(b); // <- UFCS works, as expected
> S s2 = a + b; // ERROR: doesn't work! why not? the rewrite should work
> via UFCS as above...
> }
>
> I can't imagine any good reason his experiment should have failed. I would
> want this too when extern to a C lib; it hasn't come up for me before, but
> if it did, I would log a bug instantly.
It's because the only way that you can overload operators is on the type
itself. If you could do it with free functions, then we'd get the mess of
operators working or not based on which modules you happened to import, and
there's no way to deal with symbol conflicts, because you're not directly
calling the function but instead are triggering it via a lowering that
transforms then syntax into a function call. opDispatch in particular would
be a total mess with such a scheme.
Either way, an enhancement request was created for it ages ago:
https://github.com/dlang/dmd/issues/18543
- Jonathan M Davis
More information about the Digitalmars-d
mailing list