Skip to content

Instantly share code, notes, and snippets.

@boozook
Forked from nadako/Main.hx
Last active August 29, 2015 14:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boozook/9d27b0011734bb8e33e5 to your computer and use it in GitHub Desktop.
Save boozook/9d27b0011734bb8e33e5 to your computer and use it in GitHub Desktop.
Enum fast extract
class Tools {
public static macro function extract(value:haxe.macro.Expr.ExprOf<EnumValue>, pattern:haxe.macro.Expr) {
return switch (pattern) {
case macro $a => $b:
macro switch ($value) {
case $a:
$b;
default:
throw "no match";
}
default:
var print = new haxe.macro.Printer().printExpr;
throw new haxe.macro.Expr.Error('Invalid enum value extraction pattern: "${print(pattern)}"', pattern.pos);
}
}
}
using Tools;
class Usage {
static function main() {
var a = haxe.ds.Option.Some(42);
var s = a.extract(Some(v) => {value: v});
trace(s);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment