overload binary + operator to work with different types

Marc jckj33 at gmail.com
Tue Mar 13 19:12:04 UTC 2018


On Tuesday, 13 March 2018 at 19:05:00 UTC, Jonathan M Davis wrote:
> On Tuesday, March 13, 2018 18:55:35 Marc via 
> Digitalmars-d-learn wrote:
>> I want to basically make this work:
>> >[...]
>>
>> I managed to do this:
>> > [...]
>>
>> }
>>
>> Note the ref in the fucntion return, I also want to return a
>>
>> reference to the class so that this Works:
>> > [...]
>>
>> Could someone point out how do that/what's wrong with my 
>> attempy?
>
> + is overloaded with opBinary, and += is overloaded using 
> opOpAssign:
>
> https://dlang.org/spec/operatoroverloading.html#op-assign 
> http://ddili.org/ders/d.en/operator_overloading.html
>
> - Jonathan M Davis

Soon as I posted I found out opOpAssign.
I've had got close than I thought:

>	List opOpAssign(string op)(int rhs) if(op == "+")
>	{
>		items ~= rhs;
>		return this;
>	}

Thanks!


More information about the Digitalmars-d-learn mailing list