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 case condition in return statement #4092

Closed
RepliedSage11 opened this issue Sep 12, 2024 · 3 comments
Closed

Allow case condition in return statement #4092

RepliedSage11 opened this issue Sep 12, 2024 · 3 comments
Labels
request Requests to resolve a particular developer problem

Comments

@RepliedSage11
Copy link

Trying to refactor a simple function that validates a Uri:

bool _validateUri(Uri uri) {
      return uri.path == '/path' &&
          uri.queryParameters['param1'] == 'param1' &&
          uri.queryParameters['param2'] == 'param2' &&
          uri.queryParameters['param3'] != null &&
          uri.queryParameters['param4'] != null;
}

I have followed my instincts and wrote this code using patters:

bool _validateUri(Uri uri) {
     return uri.path == '/path' && uri.queryParameters case {
         'param1': 'param1',
         'param2': 'param2',
         'param3': String _,
         'param4': String _,
     };
}

and found out that I can't use case conditions anywhere outside an if. Furthermore, when I changed the code to use an if condition I found out that I can only use a single case condition in the if.

I got this to work with a switch and a single pattern:

bool _validateUrl(Uri uri) {
    return switch (uri) {
      Uri(
        path: '/path',
        queryParameters: {
         'param1': 'param1',
          'param2': 'param2',
          'param3': String _,
          'param4': String _,
        },
      ) => true,
      _ => false,
   };
}

But this approach requires me to explicitly return true and have a default false case, when all I want is to just return a bool result of "this variable did(n't) match this pattern".

In my opinion it would be useful and intuitive to be able to use case conditions outside of if and to chain multiple conditions with case. This might be two separate issues, but this was my thought process when trying to refactor this function.

@RepliedSage11 RepliedSage11 added the request Requests to resolve a particular developer problem label Sep 12, 2024
@mateusfccp
Copy link
Contributor

mateusfccp commented Sep 12, 2024

Seems like a duplicate of (or directly related to) #3059. If we have case expressions that evaluate to bool, you can achieve this (and much more, like using them in ternary, while, for, negating etc.)

@RepliedSage11
Copy link
Author

Sorry, I tried searching for already opened issues, but didn't stumble upon that one. There is a comment describing exactly what I wanted to do. So I guess this is a duplicate, but that issue didn't have any activity for some time.

@mateusfccp
Copy link
Contributor

If you feel like #3059 solves your problem, please, close this issue. Any relevant discussion can be kept in there.

@RepliedSage11 RepliedSage11 closed this as not planned Won't fix, can't repro, duplicate, stale Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
request Requests to resolve a particular developer problem
Projects
None yet
Development

No branches or pull requests

2 participants