expression switch follow-ups
forax at univ-mlv.fr
Tue Oct 23 19:39:32 UTC 2018
i think there are two ideas that worth think about now that the expression switch will be released soon that are not related to the elephant in the room, the pattern matching.
First, a statement switch can use the arrow syntax with an expression that throw an expression.
case 10 -> 42;
default -> throw new AssertionError(); // there is no curly braces needed here
but lambdas do not allow throw as an expression.
This is not coherent, i think, we should fill that hole by allowing throw expression with no curly braces in lambdas (like we already allow function calls that return void in a lambda).
A question that have came twice at the end of my both talks about the expression switch is: why there is not arrow syntax for the try-catch/try-finally/try-with-resources ?
I've not spent a lot of time thinking about it but the syntax for try/catch or try/finally seems weird
var value =
try -> integer.parseInt(token)
catch(NumberFormatException e) -> 0;
It's a little less weird with a try-with-resources (at least when the is neither a catch nor a finally)
var text = try(var lines = Files.lines(path)) -> lines.collect(joining("."));
I think we should not try to go in that direction, because the next question will be what about if/else
var test = if (result == 42) -> true else -> null;
which is just a more verbose way to write a ternary.
More information about the amber-spec-experts