Sub topic 7
Deconstructing Records Inside Switch Patterns
Assume you know Java records, sealed interfaces, switch expressions, and basic pattern matching. The examples use Java 21 or later, where record patterns can appear inside switch cases.
What record patterns add
A record pattern does two jobs:
- It checks that a value has a particular record type.
- It extracts the record's components into local variables.
For example, Expr.Num(int value) checks for the Num record and assigns its component to value. You do not need to call value() manually.
sealed interface Expr {
record Num(int value) implements Expr {}
record Add(Expr left, Expr right) implements Expr {}
record Mul(Expr left, Expr right) implements Expr {}
}
public class Main {
static int eval(Expr expression) {
return switch (expression) {
case Expr.Num(int value) -> value;
case Expr.Add(var left, var right) -> eval(left) + eval(right);
case Expr.Mul(var left, var right) -> eval(left) * eval(right);
};
}
public static void main(String[] args) {
Expr expression = new Expr.Add(
new Expr.Num(2),
new Expr.Mul(new Expr.Num(3), new Expr.Num(4))
);
System.out.println(eval(expression)); // 14
}
}The Add and Mul patterns deconstruct their two components into left and right. Those components are still Expr values, so eval processes them recursively.
A record pattern must follow the record's component order and types. Expr.Num(int value) is valid because Num declares one int component. A pattern such as Expr.Num(String value) is invalid.
Nested patterns
Record patterns can contain other record patterns. This lets one case describe a complete shape, not just the outer record.
static String describe(Expr expression) {
return switch (expression) {
case Expr.Num(int value) ->
"number " + value;
case Expr.Add(
Expr.Num(int left),
Expr.Num(int right)) ->
"addition of two numbers: " + left + " + " + right;
case Expr.Add(var left, var right) ->
"addition of two expressions";
case Expr.Mul(var left, var right) ->
"multiplication of two expressions";
};
}The nested case matches only an Add whose left and right components are both Num records. For example, it matches Add(Num(2), Num(3)), but not Add(Num(2), Mul(Num(3), Num(4))).
Pattern order matters. The more specific nested pattern must come before the more general Expr.Add(var left, var right) pattern. If the general pattern came first, it would match every Add, making the nested case unreachable. The compiler reports that as an error.
Common errors and alternatives
You can mix record patterns with guards. A when clause adds a condition after the pattern matches.
static String classify(Expr expression) {
return switch (expression) {
case Expr.Num(int value) when value < 0 -> "negative number";
case Expr.Num(int value) -> "number";
case Expr.Add(var left, var right) -> "addition";
case Expr.Mul(var left, var right) -> "multiplication";
};
}A common error is forgetting that a nested pattern is stricter than the outer pattern. Expr.Add(Expr.Num(...), Expr.Num(...)) does not match every addition. Use var for components when their exact record type is not important.
Another error is assuming that a record pattern matches null. It does not. If the switch input itself may be null, add an explicit case null or validate the input before switching.
Exercise
Which case matches new Expr.Add(new Expr.Num(1), new Expr.Mul(new Expr.Num(2), new Expr.Num(3))): Expr.Add(Expr.Num(int a), Expr.Num(int b)) or Expr.Add(var left, var right)?
Show answer
The general Expr.Add(var left, var right) case matches it. The nested case requires both components to be Expr.Num records, but the right component is Expr.Mul.