6.4.3 Variable capture

It is possible to catch any value of a sub-pattern by matching it against an identifier:

var myTree = Node(Leaf("foo"), Node(Leaf("bar"), Leaf("foobar")));
var name = switch (myTree) {
    case Leaf(s): s;
    case Node(Leaf(s), _): s;
    case _: "none";
  }
trace(name); // foo

This would return one of the following:

  • If myTree is a Leaf, its name is returned.
  • If myTree is a Node whose left sub-tree is a Leaf, its name is returned (this will apply here, returning "foo").
  • Otherwise "none" is returned.

It is also possible to use = to capture values which are further matched:

var myTree = Node(Leaf("foo"), Node(Leaf("bar"), Leaf("foobar")));
var node = switch (myTree) {
    case Node(leafNode = Leaf("foo"), _): leafNode;
    case x: x;
  }
trace(node); // Leaf(foo)

Here, leafNode is bound to Leaf("foo") if the input matches that. In all other cases, myTree itself is returned: case x works similar to case _ in that it matches anything, but with an identifier name like x it also binds the matched value to that variable.

since Haxe 4.0.0

If the compiler detects that the name of a "catch-all" variable is very similar to an enum constructor, it will warn about a potential typo. This can be avoided by prefixing the variable identifier with var:

var myTree = Node(Leaf("foo"), Node(Leaf("bar"), Leaf("foobar")));
switch (myTree) {
  case Leaf(_):
  // ...
  case var node:
    // without var above the compiler would warn about a potential typo
    // node contains the entire matched tree
}

Any capture variable can be prefixed with var, even within sub-patterns.