Pattern match

From time to time, almost every programmer encounters the need to switch to different processing of the value depending on its type. For example:

SomeClass someObj = new SomeClass();
Object value = someOtherObject.getValue("someKey");
if (value instanceof BigDecimal) {
BigDecimal v = (BigDecimal) value;
BigDecimal vAbs = v.abs();
...
} else if (value instanceof Boolean) {
Boolean v = (Boolean)value;
boolean v1 = v.booleanValue();
...
} else if (value instanceof String) {
String v = (String) value;
String s = v.substring(3);
...
}
...

While writing such a code, you get bored pretty quickly. And that is what pattern matching is going to fix. After the feature is implemented, the preceding code example can be changed to look as follows:

SomeClass someObj = new SomeClass();
Object value = someOtherObject.getValue("someKey");
if (value instanceof BigDecimal v) {
BigDecimal vAbs = v.abs();
...
} else if (value instanceof Boolean v) {
boolean v1 = v.booleanValue();
...
} else if (value instanceof String v) {
String s = v.substring(3);
...
}
...

Nice, isn't it? It will also support the inlined version, such as the following one:

if (value instanceof String v && v.length() > 4) {
String s = v.substring(3);
...
}

This new syntax will first be allowed in an if statement and later added to a switch statement too.