Re: How do I overload += operator?

2021-01-26 Thread bachmeier via Digitalmars-d-learn
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

Re: How do I overload += operator?

2021-01-25 Thread Q. Schroll via Digitalmars-d-learn
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

Re: How do I overload += operator?

2021-01-25 Thread Jack via Digitalmars-d-learn
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`: h

Re: How do I overload += operator?

2021-01-25 Thread Jack via Digitalmars-d-learn
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 y

Re: How do I overload += operator?

2021-01-25 Thread Paul Backus via Digitalmars-d-learn
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

Re: How do I overload += operator?

2021-01-25 Thread Adam D. Ruppe via Digitalmars-d-learn
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.

How do I overload += operator?

2021-01-25 Thread Jack via Digitalmars-d-learn
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 s