On Tuesday, 16 April 2013 at 14:33:21 UTC, John Colvin wrote:
A member function cannot modify it's own 'this' pointer.

However, a free function can do it happily, which when combined with UFCS gives you the same syntax and behaviour:

class A {
        //......
}

void replace(ref A a)
{
        a = new A();
}

void main() {
        A a = new A();
        A b = a;
        assert(b is a);
        b.replace();
        assert(!(b is a));
}

http://dpaste.dzfl.pl/147f69e1

Alternatively, if you don't actually need it to be a completely seperate new object, just re-initialised, you can just call this() inside a member function. E.g.

class A
{
     public void replace()
     {
          this();
     }
}

Reply via email to