expression

Scala match/case expressions (syntax, examples) alvin July 16, 2019 - 12:03pm
Table of Contents1 - Scala match expressions2 - Aside: A quick look at Scala methods3 - Using a match expression as the body of a method4 - Handling alternate cases5 - Using if expressions in case statements6 - Even more ...

This is a lesson on Scala match/case expressions from my book, Hello, Scala.

Back to top

Scala match expressions

Scala has a concept of a match expression. In the most simple case you can use a match expression like a Java switch statement:

// i is an integer
i match {
    case 1  => println("January")
    case 2  => println("February")
    case 3  => println("March")
    case 4  => println("April")
    case 5  => println("May")
    case 6  => println("June")
    case 7  => println("July")
    case 8  => println("August")
    case 9  => println("September")
    case 10 => println("October")
    case 11 => println("November")
    case 12 => println("December")
    // catch the default with a variable so you can print it
    case _  => println("Invalid month")
}

As shown, with a match expression you write a number of case statements that you use to match possible values. In this example I match the integer values 1 through 12. Any other value falls down to the _ case, which is the catch-all, default case.

Scala best practice: Think “Expression-Oriented Programming”

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 20.3, “Scala best practice: Think "Expression-Oriented Programming".”

Problem

You’re used to writing statements in another programming language, and want to learn how to write expressions in Scala, and the benefits of the Expression-Oriented Programming (EOP) philosophy.

How to use function literals (anonymous functions) in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 9.1, “How to use function literals (anonymous functions) in Scala.”

Problem

You want to use an anonymous function — also known as a function literal — so you can pass it into a method that takes a function, or to assign it to a variable.

Solution

Given this List:

How to access the value of the default case in a Scala match expression

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is one of the shorter recipes, Recipe 3.10, “How to access the value of the default case in a Scala match expression.”

Problem

You want to access the value of the default, “catch all” case when using a match expression, but you can’t access the value when you match it with the _ wildcard syntax.

Solution

Instead of using the _ wildcard character, assign a variable name to the default case:

Scala: How to assign the result of a match expression to a variable

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is one of the shortest recipes, Recipe 3.9, “How to assign the result of a match expression to a variable.”

Problem

You want to return a value from a match expression and assign it to a variable, or use a match expression as the body of a method.

Scala: How to match multiple conditions (patterns) with one case statement

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.8, “How to match multiple conditions with one case statement.”

Problem

You have a situation where several match conditions require that the same business logic be executed, and rather than repeating your business logic for each case, you’d like to use one copy of the business logic for the matching conditions.

How to use a Scala match expression like a switch statement

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.7, “How to use a Scala match expression like a switch statement.”

Back to top

Problem

You have a situation where you want to create something like a simple Java integer-based switch statement, such as matching the days in a week, months in a year, and other situations where an integer maps to a result.

Back to top

Solution

To use a Scala match expression like a Java switch statement, use this approach:

Table of Contents

  1. Problem
  2. Solution
Back to top

Using Scala match expressions with XML

Problem: You want to use match expressions as another way to access the information contained in XML data when writing your Scala applications.

Solution

Given this XML literal:

How to enter multiline commands (statements) into the Scala REPL

When you want to test a multiline command/statement in the Scala REPL, you can easily run into a problem where the REPL gets confused, or more accurately, it attempts to evaluate your statement too early.

As a simple example of this, imagine that you want to test the Scala "if" statement syntax. You begin typing your if statement normally, but when you hit [Enter] after your second line, you'll see that everything blows up: