Created
May 16, 2014 08:42
-
-
Save Simn/a675c79ec51a10c9bdd7 to your computer and use it in GitHub Desktop.
collectFields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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