Skip to content

Instantly share code, notes, and snippets.

@anissen
Created June 11, 2014 19:40
Show Gist options
  • Save anissen/a0ed65bf146c7089893e to your computer and use it in GitHub Desktop.
Save anissen/a0ed65bf146c7089893e to your computer and use it in GitHub Desktop.
Haxe enum pattern matching
enum Tree<T> {
Leaf(v :T);
Node(l :Tree<T>, r :Tree<T>);
}
class Test {
static function main() {
var myTree = Node(
Leaf("foo"),
Node(
Leaf("bar"),
Leaf("foobar")));
trace(match(myTree)); // 2
}
static function match(t) {
return switch(t) {
case Leaf(v): v;
case Node(v1, v2): match(v1) + ":" + match(v2);
case _: "3";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment