-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow switch expression branches to have bodies #3297
Comments
Hi @ktkk i have the same problem, to overcome this I do like this final message = switch (result) {
Success(success: final success) => () {
developer.log(success);
return success
}.call(),
Error(error: final error) => throw error,
}; Hope this help you |
Hi @elliotPopina, |
Duplicate of #3117. |
You don't need to use final message = switch (result) {
Success(success: final success) => () {
developer.log(success);
return success
}(),
Error(error: final error) => throw error,
}; Although, I don't recommend. Expressions should be pure. If you need multiple lines, use a switch statement. |
Sometimes I want to do just a little bit more in one branch of a switch expression compared to another.
Let's say I want to log something when a Success branch is reached.
Currently I have 2 options:
I find the first option a bit too verbose and the second option difficult to read because it requires you to navigate to the function to know what's going on.
So why not allow switch expressions to have bodies?
This would make Dart's switch expression more similar to Rust's
match
or Kotlin'swhen
.Additionally, it could introduce implicit returns from functions like Rust or just from lambdas like Kotlin, because currently the syntax looks a bit out of place in Dart.
The text was updated successfully, but these errors were encountered: