Search code examples
macroshaxe

Haxe macro to set null to calling instance?


Is there a way of setting a null to calling instance as a result of some "macro-function-call"?

Like so:

class A {
    // ...
    macro function DestroyItself() {
        // ...
    }
}

var a:A = new A();
// ...
a.DestroyItself();
trace(a); // "null"

Solution

  • Yep:

    macro public function destroy(self:Expr) {
        return macro $self = null;
    }
    // ...
    a.destroy();
    

    In non-static macro functions first Expr argument is a ref to caller instance.