Quantcast
Channel: default
Viewing all articles
Browse latest Browse all 10

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

$
0
0

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:


Viewing all articles
Browse latest Browse all 10

Trending Articles