pattern matching
Testing a value against a shape, such as a type, a range or the values of its properties, and optionally extracting parts of it in the same step.
In more detail
Pattern matching replaces a type check followed by a cast with one construct, and the compiler tracks where the extracted parts are safe to use. In switch-like constructs, specific patterns must come before broad ones.
Examples
C#: kettle is { Price: > 20m and < 50m }Java: if (value instanceof String text && !text.isBlank()) { ... }Java 21: case Approved value -> "approved " + value.id();if (value instanceof String text && !text.isBlank()) { ... }case Approved value -> "approved " + value.id();Related terms
- type pattern — A pattern that tests a value's type and, if it matches, gives the converted value a name, as in if (boxed is int n).
- property pattern — A pattern in braces that tests an object's properties, as in kettle is { Price: > 20m }.
- relational pattern — A pattern that compares with a constant using <, >, <= or >=, such as > 20m.
- list pattern — A pattern in square brackets that matches a sequence element by element, where two dots (..) stand for any run of elements.
- switch expression — A switch construct that produces a value, with one arm per case and no fall-through between them.
- instanceof — The Java operator that checks whether a value is an instance of a type, and with a pattern also binds it to a typed variable.
- pattern guard — An extra boolean condition attached to a pattern, so a case only matches when both the type and the condition fit.
- sealed type — A class or interface that lists exactly which types may extend or implement it, so the compiler knows the complete family.
- type cast — An instruction to treat a value as a more specific type, written (String) value, which fails at runtime if the value is not that type.
- discriminated union — A type made of several object shapes that share one literal field, such as status or type, whose value tells them apart.
- infer — A keyword used inside the extends part of a conditional type to capture part of a matched type in a new type variable.
- conditional type — A type that picks one of two types depending on whether a type is assignable to another, written like a ternary expression with extends.
Taught in
- The C# Language: Types, Variables, and Flow Control — Modern .NET Core, C# & Enterprise Microservices
- Records, Pattern Matching, and Modern C# — Modern .NET Core, C# & Enterprise Microservices
- Building Routes with Controllers — Modern .NET Core, C# & Enterprise Microservices
- Sealed Types And Exhaustiveness — Modern Java: Virtual Threads & High-Throughput Services
- Pattern Matching And Text Blocks — Modern Java: Virtual Threads & High-Throughput Services
- Dates, Times And Daylight Saving — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Switch, Loops And Labels — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Inheritance, Overriding And Object's Methods — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sealed Types, Casts And Pattern Matching — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Enums With Fields, Constructors And Methods — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: Object-Oriented Java — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Locales, Resource Bundles And Formatting — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Union Types, Intersections And Property Conflicts — Production TypeScript
- Discriminated Unions: Tagged Variants And Pattern Matching — Production TypeScript
- Conditional Types: Ternary Types And Type Inference With infer — Production TypeScript
- Mapped Types, Key Remapping With as And Template Literals — Production TypeScript
References
- Pattern matching overview
- Patterns (C# reference)
- Java 21 language guide: Pattern Matching
- JEP 441: Pattern Matching for switch
- JEP 394: Pattern Matching for instanceof
- TypeScript Handbook: Conditional Types
- TypeScript Handbook: Narrowing