Skip to content

Instantly share code, notes, and snippets.

@back2dos
Last active September 3, 2023 12:18
Show Gist options
  • Save back2dos/c9410ed3ed476ecc1007 to your computer and use it in GitHub Desktop.
Save back2dos/c9410ed3ed476ecc1007 to your computer and use it in GitHub Desktop.
Store all class names for runtime use.
package ;
#if macro
import haxe.macro.Context.*;
using haxe.macro.Tools;
#end
class ClassList {
static var NAME = 'ClassList';//must be the fully qualified name of this class
macro static function build() {
onGenerate(function (types) {
var names = [],
self = getType(NAME).getClass();
for (t in types)
switch t {
case TInst(_.get() => c, _):
names.push(makeExpr(c.name, c.pos));
default:
}
self.meta.remove('classes');
self.meta.add('classes', names, self.pos);
});
return macro cast haxe.rtti.Meta.getType($p{NAME.split('.')});
}
#if !macro
static public var ALL_NAMES(default, null):Array<String> = build();
#end
}
package ;
class Usage {
static function main() {
trace(ClassList.ALL_NAMES);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment