switch statement and lambda

Remi Forax forax at univ-mlv.fr
Sat Apr 6 19:17:34 UTC 2019

Currently this code doesn't compile
  IntConsumer c = x -> switch(x) { default -> System.out.println(x); };

I believe it should because this is the basic pattern for supporting the actor model,
you consume a message and do a side effect* depending on the type of the message,
translated in Java, you want a lambda that takes a message as parameter, calls a switch to do the pattern matching and return void.
The other reason is that it's not rare to move from a switch expression to a switch statement and vice-versa when developing the code,
and adding/removing a pair of curly braces around the code you are writing because it's the body/expression of a lambda is not very user friendly.


* it's cleaner in Erlang because you have tail calls, so the side effects are hidden.

More information about the amber-spec-experts mailing list