Skip to content

Instantly share code, notes, and snippets.

@Simn
Created March 23, 2014 11:11
Show Gist options
  • Save Simn/9721730 to your computer and use it in GitHub Desktop.
Save Simn/9721730 to your computer and use it in GitHub Desktop.
Haxe macro custom generator example
-cp src
-main Main
--macro YourGenerator.use()
class Main {
static function main() {
trace("Hello world");
}
}
import haxe.macro.Context;
import haxe.macro.Type;
class YourGenerator {
static public function use() {
Context.onGenerate(generate);
}
static function generate(types:Array<Type>) {
// types contains all the compiled types, see http://api.haxe.org/haxe/macro/Type.html
// Typically you now do something like this:
for (type in types ){
switch(type) {
case TInst(c, _):
trace('Generating class $c');
case TEnum(en, _):
trace('Generating enum $en');
case TAbstract(a, _):
trace('Generating abstract $a');
case TType(t, _):
// Ignore, I guess.
case t:
trace("This never happens: " + t);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment