Java 17 switch statement is totally new! (or is it?)

I was taking a glance at Java 17 switch statement documentation, not because I’m (still) interested in Java evolutions, but because I heard rumors about a new exciting way of using the switch statement.

And rumors were true!
Now the switch statement not only can evaluated branches on values, but also on (scoped) types!. Therefore, you can avoid blocks of code like the following:

if ( o instanceof String ) { ... }
else if ( o instanceof List ) { ... }
else if ( o instanceof Integer ) { ... }


refactoring the above as:

switch( o ) {
  case String  s :  ... break;
  case List    l :  ... break;
  case Integer i :  ... break;
  default        :  ... break;



The pattern matching variable (e.g., s) is scoped to the case so there is no need of a block, but you need to avoid entering another case with another pattern matching variable or a compile time error will arise!

But where did I see something like that?
Oh, gosh, it looks very close to **Raku given when as it [is documented here](https://docs.raku.org/language/control#index-entry-control_flow__given-given={:target=”_blank”}!
Now, sorry guys, but it seems the Raku version is still much more complete and powerful.
First of all, it does not requires breaks. But allows you to “jump” into another branch by means of the special keyword proceed that is going to restart from the next when.

Conclusions

It seems to me Java is adding more and more features to become as flexible as other languages are.
But I also see how powerful other languages were years (if not decades) before Java!
Perl and Raku are probably the most complete examples I would spurt on your face.

The article Java 17 switch statement is totally new! (or is it?) has been posted by Luca Ferrari on May 26, 2022