scala match default
In Scala, the match
expression can have a default
case to handle unmatched patterns. The default
case is used when none of the previous cases match the input value. Here's an example of how to use the default
case in a match
expression in Scala:
val input: String = "unknown"
val result = input match {
case "case1" => "Result for case 1"
case "case2" => "Result for case 2"
case _ => "Default result"
}
println(result) // Output: Default result
In the above example, if the input
value matches neither "case1" nor "case2", the default
case (_
) will be executed, and the result will be "Default result" [[SOURCE 1]].