How to access the value of the default case in a Scala match expression
By Alvin Alexander. Last updated: June 13 2017
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: