Search code examples
objectddestroy

How to destruct a D object from the inside


If I have a D object, how can I destroy that D object from the inside? (Have it destroy itself)

class A {
    public void destoy() {
        // Destroy me! 
    }
}

A a = new A;
a.destroy();

Solution

  • No, there is no way to do this in D, and there are good reasons for it. This SO thread explains how it can be done in C++, and explains the threats of delete this ...

    IMHO, it is a good idea D disallows this, because it seems logical - objects should not control their own lifecycle, program should do that. Program "gave life" to objects, program should "take their lives" (garbage-collect them in this case) too.