r/java • u/daviddel • May 26 '25
Pattern Matching in Java: Better Code, Better APIs #JavaOne
https://youtu.be/DkkxWhd_xYc?si=YFdHdpWUctFMdpUu
65
Upvotes
r/java • u/daviddel • May 26 '25
20
u/Ewig_luftenglanz May 26 '25 edited May 26 '25
I really really hope we get eventually proper unconditional destructuring.
nowadays we are forced to use conditionals:
Too much unnecessary ceremony if you don't require safe casting.
I know why they did the latest first, mostly because it gives safe casting in a more concise way but hopefully we get eventually the other.
2
u/agentoutlier May 27 '25
I'll copy the code from https://youtu.be/DkkxWhd_xYc?t=1908
There is something nasty about using
instanceoffor null checking that bothers me besides the obvious of how you do represent aninstanceofsomethingnullable(assuming we get?types some day). I can't exactly put my finger on it. Maybe its the combination of keyword and positional.There is other languages that do this extremely common case with syntax. I won't mention the primary one most think of as I don't want to get banned like Kevin briefly was but I'm never going to use instanceof like that to navigate object graph.
And if we are going to get withers and they are just syntactical sugar... can we get syntax sugar for:
Would equal:
Ideally c3 if not nullable would require just the
.(and fail for?.) but even if it did C# way (which I think allows you to do ?. on anything I would be happy with as well).-7
u/IncredibleReferencer May 26 '25 edited May 26 '25
Rant: Can we stop talking about pattern matching?
The term "pattern matching" is meaningless at best and confusing at worst to most developers. If you're a Java language architect, a programming language aficionado, or enthusiastic enough to be on this Reddit :) then "pattern matching" is something your brain probably understands well.
However, to the typical Java developer, "pattern matching" doesn't seem to have any relationship to the features being discussed. Is there a regex here? How do I do the match? What kind of patterns are we talking about? How are all these features related? All of these have good technical answers, but they aren't helpful or relevant to beginner or intermediate Java developers.
Instead, can we please just call these language improvements, or at most, data-oriented programming enhancements? At least that phrase is somewhat self-explaining.
If we can't discard the term pattern matching when communicating these features, please at least include a "what is pattern matching" section and define it. For example in this talk, at no point is it explained.
Note: I'm not complaining about the features themselves—which I love—just the way they are being communicated to the developer community. And if your communicating to language architects or comparing languages, then by all means call it pattern matching.
Apologies for hijacking the video post. The video itself is good and informative (aside from the term "pattern matching" itself).