The compiler provides the function match
to check if an enum value matches a given pattern:
var myTree = Node(Leaf("foo"), Node(Leaf("bar"), Leaf("foobar"))); trace(myTree.match(Leaf(_))); // false trace(myTree.match(Node(_) | Leaf(_))); // true trace(myTree.match(Node(Leaf("foo"), _))); // true
As this function only tests if the pattern is matched, guards and variable capture are unavailable.
The match
function is equivalent to a switch
with a single case
for the given pattern, returning true
, and a default
returning false
.
var myTree = Node(Leaf("foo"), Node(Leaf("bar"), Leaf("foobar"))); myTree.match(Node(_)); // is equivalent to switch (myTree) { case Node(_): true; case _: false; }
See the EnumValue API documentation (since Haxe 3.2.1) for more information.