JEP 406 targets #Java17, which means we can start using pattern matching with `switch` (as preview)! 🥳 Here's how that's gonna work. 🧵 (For this thread to make sense, you need to know type patterns. If you don't: or nipafx.dev/java-type-patt…) 1/10
JEP 406 targets #Java17, which means we can start using pattern matching with `switch` (as preview)! 🥳 Here's how that's gonna work. 🧵 (For this thread to make sense, you need to know type patterns. If you don't: or nipafx.dev/java-type-patt…) 1/10 https://t.co/YE0NbSvCZq
Q: What can I do with this that I can't do with `if`? A: Nothing (as is generally the case for `switch`). But you can _express_ that you expect _exactly one_ of the branches to be executed. Benefits: better readability, less room for errors, compiler support, performance. 2/10
Q: So what happens if I use patterns with `switch`? A: What you would expect: The runtime finds the first pattern that matches the variable and executes the subsequent code. Details: * normal vs pattern switch * variable scope * dominance * cooperation with sealed classes 3/10
A switch that uses only constants (like strings or enum values) as labels is called a _normal switch_. One that uses only pattern as labels is called a _pattern switch_. You can't mix constant and pattern labels in the same switch - it's either or. 4/10