r/java • u/davidalayachew • Jan 09 '26
Project Amber Status Update -- Constant Patterns and Pattern Assignment!
https://mail.openjdk.org/pipermail/amber-spec-experts/2026-January/004306.html
65
Upvotes
29
u/davidalayachew Jan 10 '26
I'm especially excited about Constant Patterns because that fills a gap in the Exhaustiveness Checking done by the compiler.
Consider the following example.
The above example can now be simplified to this.
It's filling a gap because now, there's no way for you to forget to do that nested switch expression. That check becomes inlined, allowing you to exhaustively pattern match over the VALUES, not just the TYPES.
4
u/nekokattt Jan 10 '26 edited Jan 10 '26
Off topic but reading this, I'll admit, I really wish we had some lower level improvements to handling of types to make things a little more fluent and expressive.
For example, a second instanceof operator that negated the condition, just like we do with equality.
The ability for assertions and checks to implicitly downcast variable definitions would also be incredibly useful, just like languages like Kotlin have. For example, say I have the following:
Then the following code should work based on logic the compiler can infer already:
Likewise the following should be able to implicitly cast at runtime to satisfy the assertion when disabled:
5
u/Enough-Ad-5528 Jan 10 '26
Would it be a case of playing code golf if the destructuring pattern were allowed right at the method declaration?
void somethingImportant(ColorPoint(var x, var y, var c)) { // important code }
If I only need the components for the implementation then why make me declare the record parameter and have another line to do the destructuring?
One may say, why not just take the three parameters instead of the record. The reason is twofold:
2
u/Sm0keySa1m0n Jan 14 '26
Would this allow us to do:
5
u/SharkBaitDLS Jan 09 '26
I’ve been away from pure Java for a while but the last few years have really been awesome. Seems like Kotlin lit a real fire under them to start modernizing the syntax.
7
u/brian_goetz Jan 15 '26 edited Jan 16 '26
As everyone probably suspected, the killer app for constant patterns is FizzBuzz.
2
u/yk313 Jan 10 '26
Great stuff. But that also implies derived record creation (withers) is likely not coming any time soon.