| negative instanceof is a disaster I posted a bit about it here http://www.benf.org/other/cfr/java14instanceof_pattern.html it was first noted https://twitter.com/tagir_valeev/status/1210431331332689920 here but the main thing is that if the 'taken' conditional is guaranteed to exit, then scope hiding happens, if not, not. But in java if (true) {
throw new Exception();
} is not guaranteed to exit. So: https://github.com/leibnitz27/cfr_tests/blob/master/src_14/o... In case you don't want to run, as of java (build 14-ea+34-1452)
this prints: Fred WIBBLE public class InstanceOfPatternTest10 {
static String s = "WIBBLE";
public static void test(Object obj) {
if (!(obj instanceof String s)) {
throw new IllegalStateException();
}
System.out.println(s);
}
public static void test2(Object obj) {
if (!(obj instanceof String s)) {
if(true) {
throw new IllegalStateException();
}
}
System.out.println(s);
}
public static void main(String ... args) {
test("Fred");
test2("Fred");
}
}
|