opOpAssign on object properties

collerblade via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jan 8 01:22:12 PST 2017


hello guys,
i would like to have properties with /= *= += -= operators. My 
code:

struct Point {
   float x=0,y=0;

   this(float _x, float _y) {
     x=_x;
     y=_y;
   }

   //opassign for +

   //opopassign for +=
   void opOpAssign(string op=="+")(in Point p) {
     x+=p.x;
     y+=p.y;
   }
}

class A {
   public:
     Point location() const @property {
       return m_location;
     }

     void location(in Point newlocation) @property {
       m_location=newlocation;
     }

   private:
     Point m_location;
}

void main() {
   auto a= new A;
   a.location=a.location+Point(1,1); //DOES WORK
   a.location+=Point(1,1); //DOESN'T WORK
}



The problem is that this code is not working. The getter and the 
opOpAssign gets called, but the setter does not.
How can i do opOpAssign with properties??



More information about the Digitalmars-d-learn mailing list