Sobald Ansatz wäre, generische Werkzeuge zu null
jeder Instanz zu erstellen.
package ;
class Tools
{
/**
* Simply assigns null to the instance
* See more at: http://code.haxe.org/category/macros/generating-code-in-a-macro.html
*
* @param instance - Any
* @return haxe.macro.Expr
*/
public static macro function nullMe(instance : haxe.macro.Expr.ExprOf<Dynamic>) : haxe.macro.Expr
{
return macro {
${instance} = null;
};
}
}
verwenden Dies ist die using Tools;
generisch null
jede Instanz, aber ich würde dieses nicht empfehlen. Ich würde den Ansatz pro Klasse verwenden.
Main.hx
package ;
class Main {
static function main() {
// Construct
var instance = new SomeClass();
// Destroy
instance.destroy();
// Trace null
trace(instance);
}
}
SomeClass.hx
package ;
class SomeClass
{
public function new()
{
trace("Hello from SomeClass!");
}
private function preDestroy()
{
trace("The end is nigh!");
}
public macro function destroy(self : haxe.macro.Expr) : haxe.macro.Expr
{
return macro {
@:privateAccess ${self}.preDestroy();
${self} = null;
};
}
}
Zusammengestellt JS
// Generated by Haxe 3.4.2
(function() { "use strict";
var Main = function() { };
Main.main = function() {
var instance = new SomeClass();
instance.preDestroy();
instance = null;
console.log(instance);
};
var SomeClass = function() {
console.log("Hello from SomeClass!");
};
SomeClass.prototype = {
preDestroy: function() {
console.log("The end is nigh!");
}
};
Main.main();
})();
'Makro public function destroy (self: haxe.macro. Expr) {... '* * oder ** 'import haxe.macro.Expr;' – madneon