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
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
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
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
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
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.
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