-
Notifications
You must be signed in to change notification settings - Fork 204
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
Need something alternative to switch #307
Comments
This is an expression-level chained We could make We would have to allow non-constant switch expressions (which is not a problem, it just turns the switch into a guaranteed iterative operation, not a jump-table). However, we don't have a good way to refer to the switched value itself. Maybe we can introduce a variable for that ( As pointed out, number 3 alone can be written using I'm not particularly fond of number 2. It introduces a completely new syntax, a new sub-language, that we have to parse and handle. It is not particularly general (can you only do comparisons using var result = switch (x) {
case it == a: ...
case it < a || ...
...
} so we can use our existing expression grammar. Even then, it feels a little clumsy. // version 6
var result = switch {
case x == a: a
case x < a : "x < a"
case x > a || y < b: "x > a or y < b"
case x > b && y < c : "x > b and y < c"
} I want a keyword in front of the test for parsing reasons, and using This still does not allow statements in expression context, the T seq<T>(void ignore, T value) => value; and use |
What I want is a new feature to the language. |
It's true that Alternative versions of the break = expression; break label = expression;
break: expression; break label: expression;
break(expression); break label(expression); // mandatory parentheses, so probably a bad idea.
^expression; ^label: expression; // (Prefix ^ isn't used yet. Might want to reserve it for non-local returns). |
I really like @lrhn's version -- it's essentially a more readable This also means we can use them in short functions: enum ErrorType {okay, warning, critical}
Color getColor(ErrorType type) => switch (type) {
case ErrorType.okay: Colors.green
case ErrorType.warning: Colors.yellow
case ErrorType.critical: Colors.red
}; Although maybe we should use a |
Just trying some threads together. My latest proposal for pattern matching does include a |
Guys, even java already has this |
The braces inside each case in your example are unnecessary, which would make the Dart example much nicer looking. You may also be interested in the in-progress proposal to add support for switch expressions. |
Closing this because switch expressions are now enabled in the bleeding edge Dart SDK. 🎉 |
@munificent I wonder is dart will or has support for when like freezed library
|
The new switch expressions are already like Your code could be rewritten as: enum Answer {yes, no}
Answer getAnswer() => Answer.yes;
void main() {
final answer = getAnswer();
final result = switch (answer) {
Answer.yes => ...,
Answer.no => ...,
};
} |
I have seen the #27, but I think we don't need to implement the same feature with the same syntax.
Since the switch statement in dart looks old, so maybe a new expression(returns value) can be made to dart which could be used instead of the switch statement.
Following are few syntaxes I like,
1
2
3
4
5 -> (2 and 4)
6 -> (5 and 3)
Any of this syntax may be combined or altered but what I need is something that returns value based on conditions(not ?:).
I mostly like 5th and 6th approach, and would be happy if both of them or something much better or more logical is implemented.
The text was updated successfully, but these errors were encountered: