Skip to content

Fix Switch #3701

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

Closed
DartBot opened this issue Jun 17, 2012 · 5 comments
Closed

Fix Switch #3701

DartBot opened this issue Jun 17, 2012 · 5 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Jun 17, 2012

This issue was originally filed by [email protected]


This not a bug but a (highly opinionated :P) suggestion.

Dart is a new language, and with a new language based on C you have the unique opportunity to fix legacy C problems. Top of that list should be the switch statement - Dart can do better!

Lose the "label: goto" heritage of the switch statement, eliminate case fall-through and introduce proper blocks. Here is one example of a "fixed" switch statement:

switch(someVar) {
    case (1, 2, 5) {
        // this will catch up to three variations of someVar
    } case (3) {
        // no need for break, fall-though is gone
    } case (4) {
        // this is a block with its own scope
    } default {
        // default, else or case else...
    }
}

With the exception of block scope (which isn't available in JS yet anyway), the above is convertible to JavaScript.

@rakudrama
Copy link
Member

There was much discussion of switch in Issue #2047.
That ended up being merged into an issue of smaller scope which was subsequently resolved by a changes that did not address all the issues in Issue #2047.
Personally, I think http://code.google.com/p/dart/issues/detail?id=2047#c4 is the way to go, but it was resolved as requiring a break on each case.


Removed Type-Defect label.
Added Type-Enhancement, Area-Language, Triaged labels.

@DartBot
Copy link
Author

DartBot commented Jun 18, 2012

This comment was originally written by [email protected]


You're quite right, this is a duplicate and should probably be marked as such.

It seems like this subject has been discussed at length. Shame a bolder change wasn't embraced :(

@gbracha
Copy link
Contributor

gbracha commented Jun 26, 2012

  1. This issue is a duplicate.
  2. I'd happily drop switch entirely.
  3. Barring that, I'd fix it the way you suggest, but as a team we decided otherwise.
  4. In the end, switch should be mostly avoided and it scarcely matters.
    Added Duplicate label.
    Marked as being merged into Remove required break keyword from switch/case #2047.

@DartBot DartBot added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report labels Jun 26, 2012
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed type-enhancement labels Mar 1, 2016
@elijahdorman
Copy link

elijahdorman commented May 24, 2018

Implicit fallthrough is like driving a Ferrari with a broken side mirror. Sure, you can maybe use it, but it's a lot of work and it looks so damn ugly dangling down the side of the car.

There is a need for switch statements.If statements are simply not as efficient, and even if they were as efficient in the Dart compilers, they won't be when compiling to JS.

Implicit fallthrough is generally acknowledged to be a syntactic failure with new languages either adding a fallthrough statement (like Swift or Golang) or removing them entirely (like Kotlin or rust).

Case statements are generally easier to read as one-liners than if..else blocks. This is even more true when all the breaks are gone. This matters when using things like Optional types and further opens the door to add features like pattern matching (eg, Swift with tuple matching).

Finally, this breaking syntactic change is incredibly easy on the consumer. Release an automated tool that looks for .dart files, searches case by case. If that case has a break, remove it, otherwise, add a fallthrough. To my knowledge, there aren't any significant edge cases to account for when doing this at the repository level. The best time to make such a switch is before popularity skyrockets and you only have to deal with early adopters who are generally more open-minded to such changes.

Why let such a good language have such a second-rate feature looking ugly and making your code harder to read? Why not fix our Ferrari?

@lrhn
Copy link
Member

lrhn commented Jun 8, 2018

For the record: Dart does not have implicit fallthrough. The request here was purely for a different syntax to do the same thing we already do, except for not making the final break mandatory, and that's issue #2047.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

6 participants