The general practice when pattern-matching is to cover as many scenarios as possible.
If none of the specified cases match the provided input, scala.MatchError
exception is thrown during the runtime.
If you wish to match only a small subset of scenarios, it is recommended to add an additional case that handles the rest of the scenarios through the usage of a wildcard - case _ => ...
.
// Since the the following pattern-matching is non-exhaustive, a potential
// scala.MatchError exception is thrown if `returnCode` does not match any
// of the specified cases.
returnCode match {
case ALL_OK => ...
case EXIT_WITH_WARNS => ...
}
returnCode match {
case ALL_OK => ...
case EXIT_WITH_WARNS => ...
case _ => ... // fix
}
// The following approach signifies either of the 2 things -
// 1. That you do not wish to do anything if `foo` falls outside the range of
// `bar` and `baz`.
// 2. All the possible cases were handled, thus improving the readability of
// the code.
foo match {
case "bar" => ...
case "baz" => ...
case _ => None
}