Conditions · 22 of 42
Pattern matching will be familiar to anyone coming from a Haskell background, however it might look a bit weird at first for anyone coming from imperative languages.
Pattern matching in its most basic form, can look like an extended switch, with some differences:
match keyword comes after the variable (selection match compared to switch(selection))break;case _ (or case x where x can be any lower case identifier, more about it in the next slides)Pattern matching can be very powerful beyond a switch replacement, and will be explained in later slides.
val selection = "One"
selection match {
case "One" => println("You selected option one!")
case "Two" => println("You selected option two!")
case _ => println("You selected something else: ")
}