You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
} elseif (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 != operatorif(variable case! pattern when ...) { return ... } // based on is! operator, not a fan of thisif(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 configif(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 configif(item_configuration !case {
"novel_name":String name_selector,
"novel_link":String link_selector,
"image_link":String image_selector,
}) return [];
// have complex config, dont have authorif (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.
The text was updated successfully, but these errors were encountered:
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
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 thewhen
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:My code using the proposed syntax:
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.
The text was updated successfully, but these errors were encountered: