match Expressions
Branch on shape.
match Expressions is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is match?
match is Scala's powerful branching construct. You give it a value and a series of case patterns, and it runs the first matching branch.
Think of it as a more capable, expression-based version of a switch statement.
Basic Syntax
You write value match { case pattern => result }. Each case defines a pattern and the code to run when it matches.
Because match is an expression, the whole thing returns a value.
object Main {
def main(args: Array[String]): Unit = {
val n = 2
val word = n match {
case 1 => "one"
case 2 => "two"
case 3 => "three"
}
println(word)
}
}The Wildcard Case
The underscore _ is a wildcard that matches anything. Use it as a catch-all default at the end.
Without it, an unmatched value throws a MatchError at runtime.
object Main {
def main(args: Array[String]): Unit = {
val n = 9
val word = n match {
case 1 => "one"
case 2 => "two"
case _ => "many"
}
println(word)
}
}Matching Strings
Patterns are not limited to numbers. You can match string literals directly.
object Main {
def main(args: Array[String]): Unit = {
val cmd = "start"
val msg = cmd match {
case "start" => "Starting up"
case "stop" => "Shutting down"
case _ => "Unknown command"
}
println(msg)
}
}match Returns a Value
Since each branch produces a value, you can assign the result of match to a val or use it inline.
This keeps your code clean and avoids mutable variables.
object Main {
def main(args: Array[String]): Unit = {
val light = "red"
println(s"Action: " + (light match {
case "red" => "stop"
case "green" => "go"
case _ => "caution"
}))
}
}Combining Cases
You can match several values in one case using | (the pipe). All listed values lead to the same branch.
object Main {
def main(args: Array[String]): Unit = {
val day = "Sat"
val kind = day match {
case "Sat" | "Sun" => "weekend"
case _ => "weekday"
}
println(kind)
}
}match in a Function
A common pattern is to make the entire function body a single match expression. The matched result becomes the return value.
object Main {
def describe(n: Int): String = n match {
case 0 => "zero"
case 1 => "one"
case _ => "other"
}
def main(args: Array[String]): Unit = {
println(describe(0))
println(describe(5))
}
}Binding the Whole Value
You can bind the matched value to a name in the wildcard case using case other =>. Then you can use other in that branch.
object Main {
def main(args: Array[String]): Unit = {
val n = 42
val result = n match {
case 0 => "nothing"
case other => s"got $other"
}
println(result)
}
}Order Matters
Cases are tested top to bottom, and the first match wins. Put more specific patterns before general ones.
A wildcard placed too early would shadow the cases below it.
object Main {
def main(args: Array[String]): Unit = {
val n = 1
val msg = n match {
case 1 => "specific one"
case _ => "anything else"
}
println(msg)
}
}Why Use match?
Pattern matching is central to Scala because it:
- Reads clearly for multi-way branching
- Returns a value as an expression
- Scales up to types, data structures, and guards
- Can be checked for completeness by the compiler
Putting It Together
Here is a small classifier using literals, a combined case, and a default.
object Main {
def grade(score: Int): String = score match {
case 10 | 9 => "excellent"
case 8 | 7 => "good"
case _ => "keep trying"
}
def main(args: Array[String]): Unit = {
println(grade(9))
println(grade(7))
println(grade(3))
}
}Quick Check
Test your understanding of match expressions.
Recap
You learned the basics of match:
- Syntax is
value match { case pattern => result } _is the wildcard catch-all- Combine values with
| matchis an expression that returns a value- Cases are tried top to bottom; first match wins
Frequently asked questions
Is the “match Expressions” lesson free?
Yes — the full text of “match Expressions” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.
What will I learn in “match Expressions”?
Branch on shape. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Scala for Backend Engineering & Functional Programming?
No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “match Expressions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?
Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- match Expressions
- Matching Types and Values
- Guards and Binding
- Deconstruction