How do you name unused variables? If even just having to make that choice annoys you (it's not just me, right?), read on for how #JavaNext will fix that and make pattern matching (how's that related?!) more maintainable in the process. 🧵
TL;DR: Unused variables suck. They're rare now, but will be more prevalent soon. JEP 443 proposes to replace them with _ for cleaner code and better pattern matching. "This 🧵 should've been 📰" ⇝ nipafx.dev/inside-java-ne… "This 🧵 should've been 📽️" ⇝ youtube.com/watch?v=nP1k41…
Whether it's lambda parameters, catch clauses, try-with-resources, or other places: Sometimes we need to declare variables that we don't use and that sucks: * what to name them? * visual clutter / distraction * compiler / linter warnings
And it's only going to get worse! When destructuring, it's common not to need all components. Already not good, but so far that only happens with pattern matching over records, but soon we may be able to: * destructure "regular" objects * use destructuring in other places
So JEP 443 (openjdk.org/jeps/443) proposes unnamed variables and patterns. In a nutshell: * Use `_` instead of variable name (optionally with `var`) or nested pattern. * You can't refer to it, i.e. no `_ = "foo"` or `_.size()`. * Multiple `_` in the same scope work.
@nipafx But why should destructuring require me to specify all components? That would mean, I need to change my code if I add say a field to a record.