Learn
Lesson 8

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:

main.osl
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)
Click the code to edit it

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:

main.osl
int code = 404
string label = match code (
  200 -> "ok"
  404 -> "not found"
)
Click the code to edit it

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.

main.osl
boolean online = false
log match online (
  true -> "connected"
  false -> "offline"
)
Click the code to edit it

Arms with more to do#

When an arm needs more than one expression, give it a block and return the value from it:

main.osl
int status = 503
string message = match status (
  200 -> "ok"
  _ -> (
    log "unexpected status " ++ status
    return "error"
  )
)
log message
Click the code to edit it

switch 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:

main.osl
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")
Click the code to edit it

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:

main.osl
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")
Click the code to edit it
Quick check

What does this print?

osl
log match 4 (
  1 -> "one"
  2 -> "two"
  _ -> "many"
)
Your turn

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".

main.osl
Ctrl Enter runs
Expected output
scissors
rock
paper
nothing
0 of 24 lessons done