[Blog post] Operator overloading for structs in D
Minas Mina via Digitalmars-d-announce
digitalmars-d-announce at puremagic.com
Thu Jun 2 12:19:44 PDT 2016
On Thursday, 2 June 2016 at 18:55:36 UTC, Steven Schveighoffer
wrote:
> On 6/2/16 2:34 PM, Minas Mina wrote:
>> [...]
>
> Cool. You missed one very significant thing. That is using
> mixins to take advantage of the operator string.
>
> For example, opOpAssign can be done in one step:
>
> ref Rational opOpAssign(string op)(auto ref Rational r)
> if(op == "+" || op == "-" || op == "/" || op == "*")
> {
> mixin("auto tmp = this " ~ op ~ " r;");
> _n = tmp.n;
> _d = tmp.d;
> return this;
> }
>
> This is the whole reason, BTW, that operator overloads were
> changed from the original D1 style versions.
>
> Some more examples of your Rational type in action would be
> good too.
>
> -Steve
Thanks for the feedback! I will update the post tomorrow or on
Saturday.
More information about the Digitalmars-d-announce
mailing list