Thread overview
How do I overload += operator?
Jan 25, 2021
Jack
Jan 25, 2021
Adam D. Ruppe
Jan 25, 2021
Jack
Jan 25, 2021
Q. Schroll
Jan 25, 2021
Paul Backus
Jan 25, 2021
Jack
Jan 26, 2021
bachmeier
January 25, 2021
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;
    }
}


January 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.
January 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
January 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:
>>     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
January 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:
>> 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
January 25, 2021
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.
January 26, 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