digitalmars.D.learn - How do I overload += operator?
- Jack (41/41) Jan 25 2021 I'd like to make this work s += 10 where s is a struct. How can I
- Adam D. Ruppe (7/8) Jan 25 2021 try
- Jack (2/10) Jan 25 2021 it did work, thanks. That naming is confusing
- Q. Schroll (10/11) Jan 25 2021 op: it is an operator method
- Paul Backus (4/6) Jan 25 2021 +=, -=, *=, and other compound assignment operators can be
- Jack (2/8) Jan 25 2021 thanks
- bachmeier (5/7) Jan 26 2021 You have your answer, but someone else might come upon this in
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
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
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:it did work, thanks. That naming is confusingauto 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
On Monday, 25 January 2021 at 21:53:15 UTC, Jack wrote:That naming is confusingop: 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
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
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:thanksI'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
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