Matching Types and Values
Type patterns.
Matching Types and Values is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 2 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.
Type Patterns
Beyond matching literal values, match can branch on the type of a value. This is called a type pattern.
You write case x: Type => and the branch runs when the value is of that type, binding it to x.
Matching by Type
When a value has a general type like Any, you can discover its concrete type with a type pattern.
Each case x: T checks the runtime type.
object Main {
def describe(x: Any): String = x match {
case i: Int => "an Int"
case s: String => "a String"
case _ => "something else"
}
def main(args: Array[String]): Unit = {
println(describe(5))
println(describe("hi"))
println(describe(3.14))
}
}Using the Bound Value
The bound name has the matched type, so you can call its methods safely in that branch.
No casting needed: the compiler knows the type inside the case.
object Main {
def shout(x: Any): String = x match {
case s: String => s.toUpperCase
case i: Int => (i * 2).toString
case _ => "?"
}
def main(args: Array[String]): Unit = {
println(shout("hello"))
println(shout(21))
}
}Type and Value Together
You can mix value patterns and type patterns in the same match. Value patterns are usually more specific, so place them first.
object Main {
def check(x: Any): String = x match {
case 0 => "zero int"
case i: Int => s"nonzero int $i"
case _ => "not an int"
}
def main(args: Array[String]): Unit = {
println(check(0))
println(check(7))
println(check("x"))
}
}Matching Double and Boolean
Type patterns work for any type, including Double, Boolean, and Char.
object Main {
def kind(x: Any): String = x match {
case d: Double => s"double $d"
case b: Boolean => s"bool $b"
case _ => "other"
}
def main(args: Array[String]): Unit = {
println(kind(2.5))
println(kind(true))
}
}Matching Collections by Type
You can also match container types like List. Here we distinguish a list from other values.
object Main {
def info(x: Any): String = x match {
case l: List[_] => s"a list of size ${l.size}"
case _ => "not a list"
}
def main(args: Array[String]): Unit = {
println(info(List(1, 2, 3)))
println(info("nope"))
}
}A Common Use: Handling Any
Type patterns shine when a function receives Any and must react differently per type, such as logging or serialization.
object Main {
def render(x: Any): String = x match {
case i: Int => s"int=$i"
case d: Double => s"dbl=$d"
case s: String => s"str=$s"
case _ => "unknown"
}
def main(args: Array[String]): Unit = {
List(1, 2.0, "three").foreach(v => println(render(v)))
}
}Order Still Matters
Like all matches, type patterns are checked top to bottom. A broader type placed first can hide a narrower one below.
Always order from most specific to most general.
object Main {
def label(x: Any): String = x match {
case s: String => "string first"
case _: Any => "anything"
}
def main(args: Array[String]): Unit = {
println(label("hi"))
println(label(99))
}
}Type Erasure Caveat
At runtime, generic type parameters are erased. So List[Int] and List[String] look the same. Matching on List[Int] only checks that it is a List.
Use List[_] to be explicit and avoid warnings.
Why Type Patterns?
Type patterns let you:
- Safely narrow a general type without casting
- Handle heterogeneous values cleanly
- Combine with value patterns and guards
They are a stepping stone to matching on case classes and ADTs.
Putting It Together
Combine value and type patterns to classify mixed input.
object Main {
def classify(x: Any): String = x match {
case 0 => "zero"
case i: Int => "int"
case s: String => "text of length " + s.length
case _ => "other"
}
def main(args: Array[String]): Unit = {
println(classify(0))
println(classify(5))
println(classify("hey"))
}
}Quick Check
Test your knowledge of type patterns.
Recap
You learned to match on types:
case x: Typematches by runtime type and bindsx- The bound value has the matched type, so no cast is needed
- Mix value and type patterns, most specific first
- Generic parameters are erased, so use
List[_]
Frequently asked questions
Is the “Matching Types and Values” lesson free?
Yes — the full text of “Matching Types and Values” 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 “Matching Types and Values”?
Type patterns. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Matching Types and Values” 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