Scala match/case expressions (syntax, examples)

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.

Yoda Conditions alvin May 5, 2019 - 9:15pm

I had never heard of the term “Yoda Conditions” until now, but I have seen them in some Java code where programmers put the constant first in an effort to avoid null pointer exceptions.

A better test for detecting Unix operating systems in an Ant build script

In several previous tutorials (see my references below) about testing for operating systems within Ant build scripts, and then conditionally executing targets based on the results of those tests, I noted that Mac OS X operating systems respond to both Mac and Unix test conditions based on the Ant "os family" test. I mentioned that I thought this behavior was probably correct, because Mac OS X is built an a Unix base (BSD, to be specific).

Ant FAQ: How to determine the platform operating system in an Ant build script


You're creating an Ant build script, and you need to determine the operating system the script is running on, so you can make conditional decisions within the build script. You typically want/need to do this if you're going to run tasks/targets that are different for each operating system (Mac, Windows, Unix, etc.).

Perl ‘equals’ FAQ: What is true and false in Perl?

Perl true/false FAQ: What is true in Perl? What is false in Perl?

The Perl programming language is a little unusual in not having true and false boolean operators. Because of this, I can never seem to remember what equates to true and false in Perl, so I decided to create this page.

What is true/false in Perl

In short, the following elements evalue to false in Perl:

The Java ternary operator examples

Summary: This tutorial shares examples of the Java ternary operator syntax.

Interested in saying a lot while writing a little? In a single line of code, the Java ternary operator let's you assign a value to a variable based on a boolean expression (either a boolean field, or a statement that evaluates to a boolean result).

At its most basic, the ternary operator (also known as the conditional operator) can be used as an alternative to the Java if/then/else syntax, but it goes beyond that, and can even be used on the right hand side of Java statements.