Created
June 11, 2014 19:40
-
-
Save anissen/a0ed65bf146c7089893e to your computer and use it in GitHub Desktop.
Haxe enum pattern matching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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