Skip to content

Instantly share code, notes, and snippets.

@Simn
Created May 16, 2014 08:42
Show Gist options
  • Save Simn/a675c79ec51a10c9bdd7 to your computer and use it in GitHub Desktop.
Save Simn/a675c79ec51a10c9bdd7 to your computer and use it in GitHub Desktop.
collectFields
class Main {
static var fields = MyMacro.buildFieldsArray();
static function main() {
trace(fields); // [x,y,test,fields,main]
}
var x:Int;
var y:String;
function test() {
}
}
import haxe.macro.Expr;
import haxe.macro.Context;
import haxe.macro.Type;
class MyMacro {
macro static public function buildFieldsArray() {
var c = Context.getLocalClass().get();
var fieldNames = collectFields(c);
return macro $v{fieldNames};
}
static function collectFields(c:ClassType) {
var ret = [];
for (f in c.fields.get()) {
ret.push(f.name);
}
for (f in c.statics.get()) {
ret.push(f.name);
}
if (c.constructor != null) {
ret.push(c.constructor.get().name);
}
return ret;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment