Skip to content
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

Closed
ktkk opened this issue Aug 21, 2023 · 4 comments
Closed

Allow switch expression branches to have bodies #3297

ktkk opened this issue Aug 21, 2023 · 4 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@ktkk
Copy link

ktkk commented Aug 21, 2023

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:

  1. Convert the switch expression to a switch statement:
final String message;
switch (result) {
    case Success(success: final success):
        developer.log(success);
        message = success;
    case Error(error: final error):
        throw error;
}
  1. Write a separate function for the Success branch
final message = switch (result) {
    Success(success: final success) => _logAndReturn(success),
    Error(error: final error) => throw error,
};

String _logAndReturn(String message) {
    developer.log(message);
    return message;
}

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?

final message = switch (result) {
    Success(success: final success) => {
        developer.log(success);
        success
    },
    Error(error: final error) => throw error,
};

This would make Dart's switch expression more similar to Rust's match or Kotlin's when.
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.

@ktkk ktkk added the feature Proposed language feature that solves one or more problems label Aug 21, 2023
@elliotPopina
Copy link

elliotPopina commented Aug 21, 2023

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

@ktkk
Copy link
Author

ktkk commented Aug 21, 2023

Hi @elliotPopina,
That's indeed a good solution.
Would still be nice if this was a native language feature instead of a workaround like this :)

@mateusfccp
Copy link
Contributor

Duplicate of #3117.

@mateusfccp
Copy link
Contributor

mateusfccp commented Aug 21, 2023

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

You don't need to use .call():

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.

@ktkk ktkk closed this as completed Aug 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
None yet
Development

No branches or pull requests

3 participants