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
@nipafx You can assign a variable from a switch-expression. You can't from an 'if'. I would say that's not nothing 🤣
@nipafx What can I do with this that I can't do by putting that area method on Circle and Square instead not using a switch? I get it you needed an example but... try to find an example that does not encourage bad coding practice.