Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Conditionals  ›  Capstone Project

match: Choosing by Value

Conditionals 14 minA match compares one value against cases from top to bottom, can use alternatives and guards, and returns the value of the first case that fits.

match: Choosing by Value

The club's weekly plan depends on the day: intervals on Tuesday, a long run at the weekend, easy running otherwise. Writing that as if day == "Tue" … else if day == "Sat" … works, but it repeats day == on every line. A match names the value once and lists the cases.

The rule

Name the value once, list the cases, end with _.

val day = "Sat"
val session = day match
  case "Tue" => "track intervals"
  case "Sat" | "Sun" => "long run"
  case _ => "easy run or rest"
// session is "long run"
  • day match starts the match. Each case gives a pattern, an arrow =>, and the value to produce.
  • Cases are tried top to bottom, and the first that fits wins. There is no fall-through into the next case, so no break is needed.
  • "Sat" | "Sun" lists two alternatives; either one fits that case.
  • case _ fits anything. It plays the role of the final else.
  • Like if, a match is an expression, and here every case gives a String.

A guard adds a condition to a case with if:

val label = km match
  case 0.0 => "rest day"
  case k if k >= 21.1 => "half marathon"
  case k if k >= 10 => "long run"
  case _ => "easy run"

Here k is a new name for the value being matched, and the guard decides whether the case fits.

If no case fits, the program throws a MatchError at run time. That is why a match on open-ended values, such as strings or numbers, should end with case _.

This is only a first taste. The Pattern Matching topic later in the roadmap shows how match can also take data apart.

The shortcut

Write the specific cases first and the catch-all last, just as in an else if chain. Then ask: "Can any value reach the bottom without a case?" If yes, add case _.

Planning MondayStep 1 of 4
  1. Name the value

    "Mon" match starts checking the cases.

Drills

Answer fast — trust the rule.

quick drill

In the session match above, what is the session for "Sun"?

quick drill

What does case _ do?

quick drill

A match on day names has no case _, and the value is "Mon", which no case lists. What happens?

quick drill

Using the label match above, what does a 15.0 km run get?

quick drill

After a case in a Scala match runs, what happens next?

Remember

  • value match tries cases top to bottom, and the first fitting case gives the value.
  • Specific cases first, case _ last; use guards such as case k if k >= 10 for ranges.
  • Trap: leaving out case _ on open-ended values means a MatchError at run time.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainThe weekly plan as a match

Write a match that turns a day name into the club's session: "track intervals" on Tue, "long run" on Sat or Sun, "easy run or rest" otherwise. Explain how the cases are tried, what the alternative pattern and the underscore do, and what happens if the underscore case is removed.