by jzer0cool · 5y ago · 0 comments
I like learned on the front-page example a multi-state (?) switch statement. What is this called and any other languages support this?
switch [playerA.move, playerB.move] { case [Rock, Scissors] | [Paper, Rock] | [Scissors, Paper]: Winner(playerA);
...
> How does this work, do you code and specify a target output?
Indeed. You can specify multiple targets on multiple builds if your code is compatible with all of them (which happens without too much work when working with libs that do the abstraction for you, like openfl or heaps -- and yeah, that's mostly for games).
switch [playerA.move, playerB.move]
case [x, x]: Tied()
case [Rock, x]: Winner(if x == Scissors then playerA else playerB)0. https://haxe.org/manual/lf-pattern-matching-structure.html
let a = "Rock";
let b = "Paper";
let result = match (a,b) {
("Scissors", "Paper") => "A wins",
("Paper", "Rock") => "A wins",
("Rock", "Scissors") => "A wins",
("Paper", "Scissors") => "B wins",
("Rock", "Paper") => "B wins",
("Scissors", "Rock") => "B wins",
_ => "Draw"
};