Haxe Code Cookbook
Haxe programming cookbookMacrosExtract values from known enum instances

Extract values from known enum instances

Reading time: 0.5 minute

Sometimes we have an instance of enum that is of known constructor (or we only accept that constructor) and we want to extract values from that instance. Normally, to do this, we have to use pattern matching (switch), however it's quite verbose, so we can instead use this macro static extension method that will generate switch for us.


#if macro
import haxe.macro.Expr;

class Tools {
  public static macro function extract(value:ExprOf<EnumValue>, pattern:Expr):Expr {
    switch (pattern) {
      case macro $a => $b:
        return macro switch ($value) {
          case $a: $b;
          default: throw "no match";
        throw new Error("Invalid enum value extraction pattern", pattern.pos);


using Tools;

class Main {
  static function main() {
    var opt = haxe.ds.Option.Some(10);
    var val = opt.extract(Some(v) => v);
    trace(val == 10); // true

Last modified:
Category:  Macros