On Tuesday, March 13, 2018 18:55:35 Marc via Digitalmars-d-learn wrote:
> I want to basically make this work:
> >auto l = new List();
> >l += 5;
>
> I managed to do this:
> >class List
> >{
> >
> > int[] items;
> > ref List opBinary(string op)(int rhs) if(op == "+")
> > {
> >
> >     items ~= rhs;
> >     return *this;
> >
> > }
>
> }
>
> Note the ref in the fucntion return, I also want to return a
>
> reference to the class so that this Works:
> > l += 5 + 8 + 9 ... ;
>
> 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

Reply via email to