Operator Overloading - Only for Classes and Structs?
monkyyy
crazymonkyyy at gmail.com
Sun Jun 22 22:00:47 UTC 2025
On Sunday, 22 June 2025 at 15:04:27 UTC, matheus wrote:
> Hi, could someone please tell me why operador overloading
> (https://dlang.org/spec/operatoroverloading.html) is relegated
> only to classes and structs?
>
> "1 Operator overloading is accomplished by rewriting operators
> whose operands are class or struct objects into calls to
> specially named members. No additional syntax is used."
>
> I'd like to overload "~" to concatenate a string with a integer
> and do something else, like:
>
> auto s = "1" ~ 2;
>
> But what I've tried so far didn't work.
>
> If that constraint (Classes and Structs) is true, why?
>
> Thanks,
>
> Matheus.
The explanations you'll get never satisfied me. I feel like
safety is thrown around when the spec is often written to match
the previous behavior
If you need to get something working inside some already written
code I suggest alias this for bandaids; opdispath for major
redesigns.
More information about the Digitalmars-d-learn
mailing list