Match and switch
Choose between many cases with match, which gives a value, and switch, which runs code.
A chain of else if works, but when you're comparing one value against many possibilities, match and switch say it more clearly.
match gives a value#
match compares a value against each pattern in turn and gives back the result of the first one that fits. _ matches anything, so it catches every case not listed:
def dayName(int day) string -> match day (
1 -> "Monday"
2 -> "Tuesday"
3 -> "Wednesday"
4 -> "Thursday"
5 -> "Friday"
_ -> "the weekend"
)
log dayName(3)
log dayName(7)match is an expression, so it goes anywhere a value can: after =, in a log, as an argument, or as the whole body of a function, as here.
Covering every case#
The compiler checks that a match covers everything its value could be. For a number or string that means you need a _ arm, because there are always more values than the ones you've listed:
int code = 404
string label = match code (
200 -> "ok"
404 -> "not found"
)For a boolean, true and false are enough, and for an enum you list its variants. If you add a variant later, every match that forgot it stops compiling, which is exactly where you want to be told.
boolean online = false
log match online (
true -> "connected"
false -> "offline"
)Arms with more to do#
When an arm needs more than one expression, give it a block and return the value from it:
int status = 503
string message = match status (
200 -> "ok"
_ -> (
log "unexpected status " ++ status
return "error"
)
)
log messageswitch runs code#
switch is for when each case does something rather than producing a value. It runs the statements under the first case that matches, and default runs when none do. Cases written one after another share the code beneath them:
def react(string command) (
switch command (
case "start"
log "starting"
case "stop"
case "quit"
log "stopping"
default
log "unknown command: " ++ command
)
)
react("start")
react("quit")
react("dance")Only one case ever runs, so there's no falling through to the next one by accident. break leaves a case early.
Stacking cases is also how to give several values the same treatment, which a match arm can't do:
def kind(string letter) string (
switch letter.toLower() (
case "a"
case "e"
case "i"
case "o"
case "u"
return "vowel"
default
return "consonant"
)
)
log kind("E")
log kind("z")What does this print?
log match 4 (
1 -> "one"
2 -> "two"
_ -> "many"
)Rock, paper, scissors
Finish beats so it returns what each move beats: rock beats scissors, paper beats rock and scissors beats paper. Anything else beats "nothing".
scissors rock paper nothing