Case Classes and Pattern Matching
Pattern Matching
Choose a result by matching a value against cases.
Pattern Matching
PatternMatch.scala
object Main {
def main(args: Array[String]): Unit = {
val code =
val label = code match {
case 1 => "one"
case 2 => "two"
case 3 => "three"
case _ => "other"
}
println("code=" + code)
println("label=" + label)
}
}
object Main {
def main(args: Array[String]): Unit = {
val code =
val label = code match {
case 1 => "one"
case 2 => "two"
case 3 => "three"
case _ => "other"
}
println("code=" + code)
println("label=" + label)
}
}
object Main {
def main(args: Array[String]): Unit = {
val code =
val label = code match {
case 1 => "one"
case 2 => "two"
case 3 => "three"
case _ => "other"
}
println("code=" + code)
println("label=" + label)
}
}
object Main {
def main(args: Array[String]): Unit = {
val code =
val label = code match {
case 1 => "one"
case 2 => "two"
case 3 => "three"
case _ => "other"
}
println("code=" + code)
println("label=" + label)
}
}
pattern-match
A `match` expression tests a value against ordered cases and returns the first match. The `_` case is the catch-all default.