www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do I overload += operator?

reply Jack <jckj33 gmail.com> writes:
I'd like to make this work s += 10 where s is a struct. How can I 
do that?
this isn't working:

     auto ref opAssign(string op, T)(T value)
     if(op == "+")
     {
         m += value;
         return this;
     }

the compiler didn't consider that overload and return:

d.d(34): Error: s is not a scalar, it is a S

full code:

     auto s = S(10);
     writeln(--s);
     s += 5;
     writeln(s);

struct S
{
     int m;

     int opUnary(string op)()
     {
         static if(op == "--")
             return --m;
         else static if(op == "++")
             return ++m;
         else
             static assert(0, "unsupported operator");
     }

     void opBinary(string op, R)(const R rhs)
     if(op == "+")
     {
         m += rhs;
         return this;
     }

     auto ref opAssign(string op, T)(T value)
     if(op == "+")
     {
         m += value;
         return this;
     }
}
Jan 25 2021
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Monday, 25 January 2021 at 17:09:22 UTC, Jack wrote:
     auto ref opAssign(string op, T)(T value)
try opOpAssign instead opAssign is for = opOpAssign is for +=, -=, etc. It might be some variation but I think it works if you just rename it.
Jan 25 2021
parent reply Jack <jckj33 gmail.com> writes:
On Monday, 25 January 2021 at 17:11:41 UTC, Adam D. Ruppe wrote:
 On Monday, 25 January 2021 at 17:09:22 UTC, Jack wrote:
     auto ref opAssign(string op, T)(T value)
try opOpAssign instead opAssign is for = opOpAssign is for +=, -=, etc. It might be some variation but I think it works if you just rename it.
it did work, thanks. That naming is confusing
Jan 25 2021
parent Q. Schroll <qs.il.paperinik gmail.com> writes:
On Monday, 25 January 2021 at 21:53:15 UTC, Jack wrote:
 That naming is confusing
op: it is an operator method Op: it takes an operator as a parameter Assign: kinda obvious. As an example, there are opIndex, opIndexAssign and opIndexOpAssign. opIndex overloads obj[i]. opIndexAssign overloads obj[i] = rhs, and opIndexOpAssign overloads opj[i] += rhs. Maybe, in the greater scheme, the naming makes more sense to you.
Jan 25 2021
prev sibling next sibling parent reply Paul Backus <snarwin gmail.com> writes:
On Monday, 25 January 2021 at 17:09:22 UTC, Jack wrote:
 I'd like to make this work s += 10 where s is a struct. How can 
 I do that?
+=, -=, *=, and other compound assignment operators can be overloaded by defining `opOpAssign`: https://dlang.org/spec/operatoroverloading.html#op-assign
Jan 25 2021
parent Jack <jckj33 gmail.com> writes:
On Monday, 25 January 2021 at 17:12:47 UTC, Paul Backus wrote:
 On Monday, 25 January 2021 at 17:09:22 UTC, Jack wrote:
 I'd like to make this work s += 10 where s is a struct. How 
 can I do that?
+=, -=, *=, and other compound assignment operators can be overloaded by defining `opOpAssign`: https://dlang.org/spec/operatoroverloading.html#op-assign
thanks
Jan 25 2021
prev sibling parent bachmeier <no spam.net> writes:
On Monday, 25 January 2021 at 17:09:22 UTC, Jack wrote:
 I'd like to make this work s += 10 where s is a struct. How can 
 I do that?
You have your answer, but someone else might come upon this in the future, so here's a link to the clearest explanation of operator overloading for someone new to the language: http://ddili.org/ders/d.en/operator_overloading.html
Jan 26 2021