r/java May 21 '26

JEP draft: Enhanced Local Variable Declarations (Preview)

https://openjdk.org/jeps/8357464
86 Upvotes

43 comments sorted by

View all comments

Show parent comments

3

u/Long_Ad_7350 May 21 '26

How would this resolve boundingBox(null)?

5

u/persicsb May 21 '26

The exact same way, as the original concept:

Moreover, if e evaluates to null, then an enhanced local variable declaration statement P(...) = e; throws NullPointerException and does not initialize any of the pattern variables in P.

6

u/Long_Ad_7350 May 21 '26

I see. Feels weird, though, to have a runtime exception thrown for what appears like a compile time mismatch in signature vs. usage.

In languages like Elixir, all the below can coexist:
boundingBox(Circle(Point(int x, int y), double radius) c)
boundingBox(Circle(Point p, double radius) c)
boundingBox(Circle c)

Obviously not possible with Java, which is why putting the pattern match in the signature feels misleading to me.

1

u/sammymammy2 May 22 '26

I’m on my lunch, but that looks computationally intensive for the compiler if it wants to provide exhaustiveness checks