Pattern Matching for instanceof - compile-time error for a [type] instanceof [subtype]

97 views Asked by At

Reading this JDK Enhancement Proposal for Pattern Matching for instanceof:

https://openjdk.org/jeps/394

Make it a compile-time error for a pattern instanceof expression to compare an expression of type S against a pattern of type T, where S is a subtype of T. (This instanceof expression will always succeed and is then pointless. The opposite case, where a pattern match will always fail, is already a compile-time error.)

Then as Integer is a subtype of Number, I would expect a compile-time error for this code, but it works:

Integer n = 1;
if (n instanceof Number) {
    System.out.println("hi");
}

I would expect the compile-time error because as the JEP pointed this code is pointless, alsways succeed.

0

There are 0 answers