Created
May 29, 2014 12:49
-
-
Save nadako/bea417991fb0a9f21767 to your computer and use it in GitHub Desktop.
Get @:enum abstract values
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.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