Skip to content

Instantly share code, notes, and snippets.

@nadako
Created March 12, 2014 18:14
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/9512885 to your computer and use it in GitHub Desktop.
Save nadako/9512885 to your computer and use it in GitHub Desktop.
import haxe.macro.Context;
import haxe.macro.Expr;
class EnumAbstractMacro
{
public static function build():Array<Field>
{
var fields = Context.getBuildFields();
var values = [];
for (field in fields)
{
if (!hasMeta(field.meta, ":enum") || !hasMeta(field.meta, ":impl"))
continue;
switch (field.kind)
{
case FVar(_, {expr: ECast(e, _)}):
values.push(e);
default: throw false;
}
}
fields.push({
name: "validValues",
access: [APublic, AStatic],
pos: Context.currentPos(),
kind: FVar(null, macro $a{values})
});
return fields;
}
static function hasMeta(meta:Metadata, name:String):Bool
{
for (m in meta)
{
if (m.name == name)
return true;
}
return false;
}
}
@:build(EnumAbstractMacro.build())
@:enum abstract MyEnum(Int)
{
var A = 1;
var B = 2;
var C = 3;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment