No implicit opOpAssign for structs with basic types?
Ferhat Kurtulmuş
aferust at gmail.com
Sat Apr 4 10:32:32 UTC 2020
On Saturday, 4 April 2020 at 10:22:29 UTC, Robert M. Münch wrote:
> D doesn't have implicit operators for structs?
>
> struct S {float a, float b};
> S a = {1, 5};
> S b = {2, 5);
>
> a += b;
> Error: a is not a scalar, it is a S
>
> So I really have to write an overloaded operator for such cases?
Probably I didn't understand what you mean. Sorry if this is not
the case, but this one is easy.
...
struct S {
float a;
float b;
S opOpAssign(string op)(ref S rhs) if (op == "+"){
this.a += rhs.a;
this.b += rhs.b;
return this;
}
}
void main()
{
S a = {1, 5};
S b = {2, 5};
a += b;
writeln(a);
}
...
More information about the Digitalmars-d-learn
mailing list