-
-
Save boozook/9d27b0011734bb8e33e5 to your computer and use it in GitHub Desktop.
Enum fast extract
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
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); | |
} | |
} | |
} |
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
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