-
Notifications
You must be signed in to change notification settings - Fork 205
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
jdk 12 like switch statement #27
Comments
"Enhanced Switch" seems to be the same as removing the redundant |
What I really think we should do is pattern matching, including full destructuring of objects and collections. That covers all of the use cases of switch and then some. I'm not currently looking at that as part of the "UI as code" changes because this kind of control flow doesn't seem to be a large component of Flutter UI code. |
Pardon the intrusion and raising this from the depths of the netherrealms, but I'd love to note a good use-case for a switch expression and/or pattern matching! I'm just diving into Flutter and following some tutorials that recommend the BLoC pattern for building apps. Of note, given a Stream<State> mapEventToState(Event e) async {
if (e is Foo) {
yield SomeState( ... );
} else if (e is Bar) {
yield OtherState( ... );
} else if ....
} else {
yield ErrorState();
}
} or even in many examples the yet more verbose pattern of if (e is Foo) {
yield SomeState();
return;
}
if (e is Bar) {
yield OtherState();
return;
}
... As written, this is very wordy, especially if your Bloc can handle many types of events! If we look at the state of the art in other languages, constructs like C# 8's switch expression or Rust's match expressions leap out as a fantastic way to clean these up. For example, in C# (and forgive me for making the sample more C#-ey): Iterable<State> MapEventToState(Event e)
{
yield return e switch
{
Foo _ => new SomeState( ... ),
Bar _ => new OtherState( ... ),
...
_ => new ErrorState()
}
} That is, in my opinion, a major improvement! A lot less noise, more scannable, and much less repetition. Especially handy in this case is the ability write |
Based on Kotlin switch (dynamic x) {
is! Integer -> print("x is not an integer")
isSpecialMagicNumber(x) -> print("x is magic number from function")
in 1..10 -> print("x is in the range")
in validNumbers -> print("x is in array")
!in 10..20 -> print("x is outside the range")
else -> print("none of the above")
} |
@thefuzzyorange, maybe you could re-target this issue to be about improvements of the Dart switch statement in a broader sense (that is, change the title to something like 'Improve the switch statement')? Then we could gather several issues about switch here, and maintain a good overview of the proposals: #307, #703, #745. |
Scala one is better |
Is there a status on this issue? Can we revive it? I think @sosite made an excellent point; Kotlin's
There's also another issue floating somewhere about not having to retype the first section of an enum in every case statement. I'll link it if I find it (EDIT: #357), but that also seems more relevant when we can bundle cases. Heck, even JDK 14 |
Also: @munificent I think this could easily be part of the UI as code paradigm. var myApp = MaterialApp (
// some other stuff here
home: switch (getBreakpoint()) { //where getBreakpoint is a enum of type BreakPoint
case .xL -> xLHomePage();
case .L -> LHomePage();
case .mL -> mLHomePage();
case .m -> mHomePage();
case .sM -> sMHomePage();
case .s -> sHomePage();
}
) This a small and somewhat contrived example, but it serves the purpose. If you have to switch between 3+ widgets based on a certain value, this is pretty time-saving. |
Closing this because switch expressions are now enabled in the bleeding edge Dart SDK. 🎉 |
As a proposal I'd like to suggest Dart to implement enhancements to the switch statement in the same manner as Java is doing with JDK 12.
The enhancements allow for
The new switch features are outlined in good detail here:
https://www.javacodegeeks.com/2018/09/jdk-12-switch-statements-expressions.html
All examples are from article above. I've just added them here from a high level point of view
Traditional Switch
JDK 12 Enhanced Switch
JDK 12 : New switch Expression Returning Value via break
JDK 12 : New switch Expression Returning Value via Label Rules
JDK 12 : Multiple Constants Can Be Specified for a Single case
The text was updated successfully, but these errors were encountered: