Skip to content

Instantly share code, notes, and snippets.

@nadako
Created May 29, 2014 12:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nadako/bea417991fb0a9f21767 to your computer and use it in GitHub Desktop.
Save nadako/bea417991fb0a9f21767 to your computer and use it in GitHub Desktop.
Get @:enum abstract values
import haxe.macro.Context;
import haxe.macro.Expr;
using haxe.macro.Tools;
@:enum abstract MyEnum(Int) {
var A = 1;
var B = 2;
var C = 3;
}
class Main {
static function main() {
trace(enums(MyEnum));
}
static macro function enums(e) {
var path = e.toString();
var type = Context.getType(path);
switch (type.follow()) {
case TAbstract(_.get() => ab, _) if (ab.meta.has(":enum")):
var ct = type.toComplexType();
var values = [];
for (f in ab.impl.get().statics.get())
{
if (!(f.meta.has(":enum") && f.meta.has(":impl")))
continue;
var expr = Context.getTypedExpr(f.expr());
values.push(macro ($expr : $ct));
}
return macro $a{values};
default:
throw new Error(type.toString() + " should be @:enum abstract", e.pos);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment