Summary
Support pattern matching by the instanceof
operator:
... o is a variable declared with a general type such as Object ...
if (o instanceof <type> v) {
... use the newly declared variable v
which refers to the same object as o
but with a more specific type ...
}
Problem
The instanceof
operator is typically used in code like this:
... o is a variable declared with a general type such as Object ...
if (o instanceof <type>) {
<type> v = (<type>) o;
...
}
The static type of the variable o
is too general to be useful, but this part of the program is aware that the variable o
might refer at run time to an object whose type is more specific and thus more useful. To use the object in a type-safe manner, the program must mention the more specific type three times: (1) in the instanceof
operator which examines the object referred to by o
, (2) in the declaration of a new variable, and (3) in the cast of o
. This idiom is laborious to write, which increases the chance of a mistake, and, more importantly it is boilerplate which slows down all readers.
Solution
Allow the right hand side of the instanceof
operator to take a pattern consisting of a type and a variable name. A boolean expression of the form <expr> instanceof <type> v
is evaluated as follows: if the value of <expr>
is dynamically an instance of <type>
, then declare variable v
whose static type is <type>
and initialize v
with the value of <expr>
.
The scope of variable v
includes sections of code which are executed if the result of instanceof
is true. The scope may extend beyond the statement which contains an instanceof
operator, e.g., beyond an if
statement whose conditional includes an instanceof
.
The <type>
in an instanceof
is allowed to be non-reifiable if <expr>
can be cast to <type>
without unchecked warnings. This applies to both the "traditional" form of instanceof
(<expr> instanceof <type>
) and to the "pattern-matching" form of instanceof
(<expr> instanceof <type> v
).
The Compiler Tree API is updated to model the pattern-matching form of instanceof
.
Specification
The draft specification is attached. It is also available at http://cr.openjdk.java.net/~gbierman/jep305/jep305-20191021/specs/patterns-instanceof-jls.html
The specdiff for the Compiler Tree API change is attached as specdiff.04a.zip. It is also available at http://cr.openjdk.java.net/~jlahoda/8231826/specdiff.04/overview-summary.html
- csr of
-
JDK-8231826 Implement javac changes for pattern matching for instanceof
- Resolved
- relates to
-
JDK-8235617 JLS changes for Pattern Matching for instanceof (Preview)
- Resolved
-
JDK-8242368 javac changes for pattern matching for instanceof (Second Preview)
- Closed
-
JDK-8254235 Compiler implementation of Pattern Matching for instanceof (Final)
- Closed