Skip to content

[Feature] Support enum methods #45880

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
YazeedAlKhalaf opened this issue May 1, 2021 · 2 comments
Closed

[Feature] Support enum methods #45880

YazeedAlKhalaf opened this issue May 1, 2021 · 2 comments
Labels
closed-duplicate Closed in favor of an existing report

Comments

@YazeedAlKhalaf
Copy link

YazeedAlKhalaf commented May 1, 2021

The title may be misleading but not sure what this is called 🙈.

For example, if I want to use an enum as part of my data classes I have to create global methods to convert the enum value to a string and then a string to an enum value.

I believe this will make using enums a lot easier.

Now:

enum FlutterChannel {
  dev,
  beta,
  stable,
}

String convertFlutterChannelEnumToString(FlutterChannel flutterChannel) {
  switch (flutterChannel) {
    case FlutterChannel.dev:
      return "dev";
    case FlutterChannel.beta:
      return "beta";
    case FlutterChannel.stable:
      return "stable";
    default:
      return "stable";
  }
}

FlutterChannel convertStringToFlutterChannelEnum(String flutterChannelString) {
  switch (flutterChannelString) {
    case "dev":
      return FlutterChannel.dev;
    case "beta":
      return FlutterChannel.beta;
    case "stable":
      return FlutterChannel.stable;
    default:
      return FlutterChannel.stable;
  }
}

Proposed:

enum FlutterChannel {
  @override
  List<dynamic> get values => <dynamic>[
    dev,
    beta,
    stable,
  ];

  factory FlutterChannel.fromString(String flutterChannelString) {
    switch (flutterChannelString) {
      case "dev":
        return FlutterChannel.dev;
      case "beta":
        return FlutterChannel.beta;
      case "stable":
        return FlutterChannel.stable;
      default:
        return FlutterChannel.stable;
    }
  }

  String toString(FlutterChannel flutterChannel) {
    switch (flutterChannel) {
      case FlutterChannel.dev:
        return "dev";
      case FlutterChannel.beta:
        return "beta";
      case FlutterChannel.stable:
        return "stable";
      default:
        return "stable";
    }
  }
}
@DevNico
Copy link

DevNico commented May 3, 2021

The proposed code feels really ugly to me and I don't really see the benefits of adding such a feature. You can just write functions like:

T? enumFromString<T>(List<T> values, String value) {
  return values.firstWhereOrNull((v) => v.toString().split('.')[1] == value);
}

String enumToString(Object e) => e.toString().split('.')[1];

Usage:

enum Test { a }

void main() {
  final a = enumFromString(Test.values, enumToString(Test.a));
}

@lrhn
Copy link
Member

lrhn commented May 3, 2021

Duplicate of dart-lang/language#1570 (do put your thumbs ups there instead!)
This is a language feature, so keeping the request in the language repo.

@lrhn lrhn closed this as completed May 3, 2021
@lrhn lrhn added the closed-duplicate Closed in favor of an existing report label May 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-duplicate Closed in favor of an existing report
Projects
None yet
Development

No branches or pull requests

3 participants