COMMUNITY

Does haxe have a limit on the number of possible matches using Enums?

Given these enums:

enum Either1<A> {
  A(v:A);
}
enum Either2<A, B> {
  A(v:A);
  B(v:B);
}
enum Either3<A, B, C> {
  A(v:A);
  B(v:B);
  C(v:C);
}
enum Either4<A, B, C, D> {
  A(v:A);
  B(v:B);
  C(v:C);
  D(v:D);
}

And this function:

public static function getValue(e: Dynamic): Dynamic {
switch(Type.getEnum(e)) {
      case Either1:
        switch(e) {
          case A(a):
            a;
        }
      case Either2:
        switch(e) {
          case A(a):
            a;
          case B(b):
            b;
        }
      case Either3:
        switch(e) {
          case A(a):
            a;
          case B(b):
            b;
          case C(c):
            c;
        }
      case Either4:
        switch(e) {
          case A(a):
            a;
          case B(b):
            b;
          case C(c):
            c;
          case D(d):
            d;
        }
      case _:
        e;
    }
}

I get this compilation error on haxe 3.4.7:

Unmatched patterns: A | B | C

But, I only get this compilation error only AFTER I add the Either4 case. Up until then it compiles as expected.

Am I missing something?

I found this silly workaround. Not sure why this works at all.

switch(Type.getEnum(e)) {
      case Either1:
        switch(e) {
          case A(a):
            a;
        }
      case Either2:
        switch(e) {
          case A(a):
            a;
          case B(b):
            b;
        }
      case Either3:
        switch(e) {
          case A(a):
            a;
          case B(b):
            b;
          case C(c):
            c;
        }
      case Either4:
        switch(e) {
          case D(d):
            d;

          case v:
            switch(v) {
              case A(a):
                a;
              case B(b):
                b;
              case C(c):
                c;
            }
        }

To get rid of ambiguity, you can add the name of an enum to the names of constructors (case Either1.A, case Either2.B etc.)