Description
The motivation for this is that I'd love to be able to return from a switch
expression. i.e.:
String? someFunc(Node node) {
final err = switch(node) {
case Foo(field: String toReturn) => return toReturn,
_ => 'some error message',
};
handleErr(err);
return null;
}
Since return
introduces a statement rather than expression, this isn't possible. I see in the language spec that that throw
is an expression, making switch(x) { _ => throw 'foo' }
legal. Is there a motivation for supporting throw
and not other control flow keywords? In particular, I'm thinking return
, yield
, continue
, break
, and rethrow
. Maybe it'd be easy to redefine these as expressions, which would be backwards compatible given that Dart already has an expression statement.
For a relatively mainstream language that has pattern matching and treats continue
, break
, and return
as expressions, we can look at Rust's expression grammar.
(One other thing I'll point out with the introduction of switch expressions is that I'd love to run arbitrary groups of statements in an expression; something like #132)