Summary
Several minor bugs in the Java Language Specification for the "Pattern Matching for instanceof
" feature have been identified, and fixed. The feature will remain a preview feature for JDK 15.
Problem
As a feedback to the "Pattern Matching for instanceof
" preview feature, there were two minor bugs identified:
The following code would not compile per the spec (as it contained no handling for parenthesized expressions):
if ((o instanceof String s)) { System.err.println(s); //the binding variable does not propagate through the parenthesized expression per JLS }
The following code would not compile per the spec (as it contained no handling for labeled statements):
LABELED: if (o instanceof String s) ; else throw new IllegalStateException(); System.err.println(s); //labeled statements didn't introduce any variables into the scope, even if the nested statement would.
Solution
Sections 6.3.1.7 Parenthesized Expressions, and 6.3.2.7 Labeled Statements have been added to the specification to fix the above problems.
The feature "Pattern Matching for instanceof
" will continue to be a preview feature in JDK 15.
Specification
The updated specification is updated as patterns-instanceof-jls-20200413.zip, with a simple diff against the previous specification attached as JDK-8242368-diff.zip. The specification is also available for convenience here: http://cr.openjdk.java.net/~gbierman/jep375/jep375-20200413/specs/patterns-instanceof-jls.html
The only non-editorial changes to the specification compared to the JDK 14 version of the specification is the addition of sections 6.3.1.7 and 6.3.2.7.
- csr of
-
JDK-8242367 javac changes for pattern matching for instanceof (Second Preview)
- Closed
- relates to
-
JDK-8231827 Implement javac changes for pattern matching for instanceof (Preview)
- Closed
-
JDK-8246156 JLS changes for Pattern Matching for instanceof (Second Preview)
- Resolved
-
JDK-8254235 Compiler implementation of Pattern Matching for instanceof (Final)
- Closed
-
JDK-8240999 Implement javac changes for deconstruction patterns.
- Draft