No implicit opOpAssign for structs with basic types?

Ferhat Kurtulmuş aferust at gmail.com
Sat Apr 4 16:14:03 UTC 2020


On Saturday, 4 April 2020 at 12:14:23 UTC, Robert M. Münch wrote:
> On 2020-04-04 10:32:32 +0000, Ferhat Kurtulmuş said:
>
>> 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;
>>      }
>> }
>
> If the struct is from some 3rd party source, how can I add such 
> an operator overloading to it? Is it possible to "extend" a 
> struct later?

Oh I see what you mean now. In JavaScript you can for instance 
extend Array with an user defined method like;

Array.prototype.insert = function(index) {
     ...
     return this;
};

Of course in d CTFE helps for similar cases. But I am afraid 
operator overloading outside struct/class body is not available 
in D. It would be nice feature though.



More information about the Digitalmars-d-learn mailing list