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

Support "if - not case" statements #4064

Closed
hydro63 opened this issue Aug 29, 2024 · 2 comments
Closed

Support "if - not case" statements #4064

hydro63 opened this issue Aug 29, 2024 · 2 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@hydro63
Copy link

hydro63 commented Aug 29, 2024

Problem

I have been recently doing some web scraping and validating configuration json, and i wanted to use pattern matching to make it easier. The problem arised when i needed to do multilevel configuration validation, and i found out that there is no real way return when configuration is wrong, meaning i have to nest multiple nseted if-s for easy validation

Map<String, dynamic> configuration;
...
List<NovelData> search(...){
  if(configuration case {
    "search": {
      "site": String url,
      "name_key": String name_key,
      "pagination_key": String page_key,
      "items_container": var item_configuration
    }
  }){
    if(item_configuration case {
      "novel_name": String name_selector,
      "novel_link": String link_selector,
      "image_link": String image_selector,
      "author": String author
    }){
      // do something with author
    } else if (item_configuration case {
      "novel_name" : String name_selector,
      "novel_link": String link_selector,
      "image_link": String image_selector,
      // field "author" is optional;
    }) {
    } else {
        return [];
    }
  }
  return [];
}

It's very longwinded, and the more layers there is to validate, the more ifs are nested inside, because there is no real way to quickly return if the pattern doesnt match.

Proposal

I propose adding if - not case statement, which will run if the pattern doesn't match or when the when doesn't pass. If the pattern matches, all the variables will be declared and ready to use on the outside. Here is my proposed syntax:

if(variable !case pattern when ...) { return ... } // based on != operator
if(variable case! pattern when ...) { return ... } // based on is! operator, not a fan of this
if(variable not case pattern when ...) { return ... } // the most readable option, but additional keyword

My code using the proposed syntax:

List<NovelData> search(...){
  // dont have basic config
  if(configuration !case {
    "search": {
      "site": String url,
      "name_key": String name_key,
      "pagination_key": String page_key,
      "items_container": var item_configuration
    }
  }) return [];
  
  // have basic config, dont have complex config
  if(item_configuration !case {
    "novel_name": String name_selector,
    "novel_link": String link_selector,
    "image_link": String image_selector,
  }) return [];
  
  // have complex config, dont have author
  if (item_configuration !case {"author": String author}){
    ...
  }
  
  // have everything
  ...
  return [];
}

This way, my code is a lot easier to navigate and understand. While this example may not seem big, often times we have to validate 3 or more layers with many optional fields. In such cases, having a syntax for quick return is a big improvement.

@hydro63 hydro63 added the feature Proposed language feature that solves one or more problems label Aug 29, 2024
@hydro63 hydro63 changed the title Support if - not case statements Support "if - not case" statements Aug 29, 2024
@mateusfccp
Copy link
Contributor

Related: #2537.

@hydro63
Copy link
Author

hydro63 commented Aug 29, 2024

Issue closed due to duplicity - see #2537

@hydro63 hydro63 closed this as completed Aug 29, 2024
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

2 participants