5.17 switch

A basic switch expression starts with the switch keyword and the switch subject expression, as well as the case expressions between curly braces {}. Case expressions either start with the case keyword and are followed by a pattern expression, or consist of the default keyword. In both cases a colon : and an optional case body expression follows:

switch subject {
  case pattern1: case-body-expression-1;
  case pattern2: case-body-expression-2;
  default: default-expression;
}

Case body expressions never "fall through", so the break keyword is not supported in Haxe.

Switch expressions can be used as value; in that case the types of all case body expressions and the default expression must unify.

Each case (including the default one) is also a variable scope, which affects variable shadowing.

switch (0) {
  case 0:
    var a = "foo";
  case _:
    // This would cause a compilation error, since `a` from the previous
    // case is not accessible in this case:
    // trace(a);
}
Related content