Tutorial by Examples

Assume the following enum: enum Operation { Multiply(left : Int, right : Int); } Enum matching can be performed as follows: var result = switch(Multiply(1, 3)) { case Multiply(_, 0): 0; case Multiply(0, _): 0; case Multiply(l, r): l * r; } Ref...
Assume the following structure: var dog = { name : "Woofer", age : 7 }; Enum matching can be performed as follows: var message = switch(dog) { case { name : "Woofer" }: "I know you, Woofer!"; case _: "I don't know you, so...
var result = switch([1, 6]) { case [2, _]: "0"; case [_, 6]: "1"; case []: "2"; case [_, _, _]: "3"; case _: "4"; } References "Array matching", Haxe manual
The | operator can be used anywhere within patterns to describe multiple accepted patterns. If there is a captured variable in an or-pattern, it must appear in both its sub-patterns. var match = switch(7) { case 4 | 1: "0"; case 6 | 7: "1"; case _: "2"; ...
It is also possible to further restrict patterns with guards. These are defined by the case ... if(condition): syntax. var myArray = [7, 6]; var s = switch(myArray) { case [a, b] if (b > a): b + ">" +a; case [a, b]: b + "<=" +a; case _:...
Extractors are identified by the extractorExpression => match expression. Extractors consist of two parts, which are separated by the => operator. The left side can be any expression, where all occurrences of underscore _ are replaced with the currently matched value. The right side is a p...

Page 1 of 1