+import haxe.macro.Context;
+ * http://yal.cc/haxe-c-style-enum-macros/
+ * @author YellowAfterlife
+ public static macro function build():Array<Field> {
+ switch (Context.getLocalClass().get().kind) {
+ case KAbstractImpl(_.get() => { type: TAbstract(_.get() => { name: "Int" }, _) }): // OK
+ default: Context.error(
+ "This macro should only be applied to abstracts with base type Int",
+ var fields:Array<Field> = Context.getBuildFields();
+ var getNameCases:Array<Case> = [];
+ for (field in fields) {
+ var value:String = null;
+ case FVar(t, { expr: EConst(CInt(int)) }): { // `var some = 1;`
+ nextIndex = Std.parseInt(value) + 1;
+ case FVar(t, null): { // `var some;`
+ value = Std.string(nextIndex++);
+ field.kind = FVar(t, { expr: EConst(CInt(value)), pos: field.pos });
+ if (value != null) getNameCases.push({
+ values: [{ expr: EConst(CInt(value)), pos: field.pos }],
+ expr: { expr: EConst(CString(field.name)), pos: field.pos }
+ } // for (field in fields)
+ var getNameExpr:Expr = {
+ expr: ESwitch(macro this, getNameCases, macro null),
+ pos: Context.currentPos()
+ fields.push((macro class Magic {
+ public function getName():String return $getNameExpr;