-
Notifications
You must be signed in to change notification settings - Fork 211
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
'Switch-case' shorthand syntax #703
Comments
You can write the map literal inline, so it would be: String getValue(final int a) => const {
1 : "first",
2 : "second",
3 : "third",
}[a] ?? "${a}th"; That's not too far from the switch. It does require the expressions to be constant, or if the map is not constant, then all of the expressions are evaluated on each use. Making |
I think this has been discussed in many existing issues. I think this proposal may satisfy you? It mentions switch expressions and links to 2 related issues. It also brings them into a more comprehensive context. |
Here's a clean workaround when dealing with an enum and you want to be sure you cover all enum options (which unfortunately isn't possible with the final resultOfExpression = (){
switch(value) {
case SomeEnum.foo:
return 'fizz';
case SomeEnum.bar:
return 'buzz';
}
}(); Worth mentioning: in lots of use cases, the above can be achieved better by using enhanced enums with a member field for whatever you are trying to access. Sometimes though, this little trick can come in handy. |
Dart already does have a switch expression of sorts with nested ternary operators. final x = y ? 1 : (z ? 2 : (w ? 3 : 4)); It's cumbersome but it is the only way you can initialize const fields. In that sense a switch expression could just be syntactic sugar on the front end that transforms to nested ternary operators (and hopefully does a completeness check). Getting a bit philosophical, if you don't mind, Dart was designed to be an optimal expression of well known languages at the time it was made. It was designed, imo, to make it an easy transition from another popular languages and I think it did a marvelous job at that. Right now we are experiencing a great shift to the next generation of languages, Objc->Swift, Java->Kotlin, C++->Rust, so the question becomes "Does Dart want to maintain familiar for the last generation of languages, or does it want to be familiar to the next generation of languages?" What was odd when Dart was designed is now becoming common place. A "switch expression" (which my guess actually comes from ML's |
It also looks like this issue is just a version of #27 |
Note that with the in-progress patterns proposal, you can write: var aStr = switch (a) {
case 1 => "first";
case 2 => "second";
case 3 => "thrird";
case _ => "${a}th"
}; |
Would be cool if there could be a list somewhere of all the issues that will be fixed with records/other featuers so they can all be closed at once |
I'm trying to label all issues related to patterns and records with patterns or records. There's also a pair of "later" labels for issues related to those that likely won't be addressed in the initial release. It's probably not entirely comprehensive because there isn't always a perfect mapping between a user request and a specific language feature, but most of them should be in there. |
Closing this because records and patterns are now enabled by default on the main branch of the Dart SDK! 🎉 |
A succinct and readable way to do this is:
|
|
Yes, not only the In Dart 3.0, this is the correct syntax: var aStr = switch (a) {
1 => "first",
2 => "second",
3 => "thrird",
_ => "${a}th",
}; |
Life comes at you real fast! :D Yes, we change the switch expression syntax after I wrote that comment. Thanks for the corrections. |
This is about an antique C-syntax construction like a 'switch-case' statement. In my experience, the use case of it is often like a map some value 'A' to another value 'B'. Therefore, some modern languages try to simplify this case (influenced by functional languages like Haskell, I think). So it will be nice if Dart would have something like that.
For example, Kotlin has 'when' operator :
C# 8.0 has something the same:
Notice that in both of languages improved 'switch-case' is not a statement, but an expression that
prevent us to wrap statement into the function to get this sort of a 'mapping'.
In Dart to get this result, I need to:
Or
In other cases in my opinion it's better to avoid an antique 'switch-case' statement to improve code readeablitiy.
The text was updated successfully, but these errors were encountered: