Skip to content
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 Cases & Enum & Extension Query #1165

Open
AbhishekTF2020 opened this issue Aug 20, 2020 · 1 comment
Open

Switch Cases & Enum & Extension Query #1165

AbhishekTF2020 opened this issue Aug 20, 2020 · 1 comment
Labels
feature Proposed language feature that solves one or more problems

Comments

@AbhishekTF2020
Copy link

AbhishekTF2020 commented Aug 20, 2020

  1. Currently in dart I find we have to put scope braces and break statement in cases in switch statement.
    Can we do like in swift we have switch case as shown below.
switch index {
   case 100  :
      println( "Value of index is 100")
   case 10, 15:
      println( "Value of index is either 10 or 15")
   case 5  :
      println( "Value of index is 5")
   default :
      println( "default case")
}
  1. Enum Switch case type infer: In each switch case we have mention enumName.value. Can we do like in swift

DART

enum Status { none, run, stop, pause }

extension StatusEx on Status {
  String get name {
    switch (this) {
      case Status.run:
        return 'RUN';
      case Status.stop:
        return 'STOP';
      default:
        return "NONE";
    }
  }
}

In swift we don't have to mention enum name, we use direct .notation.

SWIFT

enum Status { none, run, stop, pause }

extension Status {
  String get name {
    switch (this) {
      case .run:
        return 'RUN';
      case .stop:
        return 'STOP';
      default:
        return "NONE";
    }
  }
}
  1. For extending Enum, can we remove mentioning new name as above?
@AbhishekTF2020 AbhishekTF2020 added the feature Proposed language feature that solves one or more problems label Aug 20, 2020
@lrhn
Copy link
Member

lrhn commented Aug 24, 2020

Dart switch cases require break, but not braces.

switch (index) {
   case 100 :
      print( "Value of index is 100");
      break;
   case 10:
   case 15:
      print( "Value of index is either 10 or 15");
      break;
   case 5 :
      print( "Value of index is 5");
      break;
   default :
      print( "default case")
}

I'd love to get rid of the requirement to have a break. I don't think we have an open issue for that.

Avoding the enum name exists as issue #357.

As for allowing unnamed extensions, you can write:

extension on Status { ... }

but that only works within the same library.
For now you need extension StatusExtension on Status { ... } because it gives the extension a name, which allows you to hide or show it in an import. An unnamed extension would be impossible to hide or show.
I'd rather improve enums so that you can declare the method directly on the enum, rather than have to add it with an extension.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
None yet
Development

No branches or pull requests

2 participants