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

Enum constructor support #570

Closed
keertip opened this issue Sep 11, 2019 · 1 comment
Closed

Enum constructor support #570

keertip opened this issue Sep 11, 2019 · 1 comment
Labels
request Requests to resolve a particular developer problem state-duplicate This issue or pull request already exists

Comments

@keertip
Copy link

keertip commented Sep 11, 2019

@pedromassango commented on Wed Sep 11 2019

I'm coming from Android development with Kotlin where we have support for constructors and properties while in Dart we don't.

In my current Flutter project I have the following code to handle my EventStatus Enum:

class EnumValues<T> {
  Map<String, T> map;
  Map<T, String> reverseMap;

  EnumValues(this.map);

  Map<T, String> get reverse {
    if (reverseMap == null) {
      reverseMap = map.map((k, v) => new MapEntry(v, k));
    }
    return reverseMap;
  }
}

class EventStatusConverter {
  static final _statusValues = new EnumValues<EventStatus>({
    'UPCOMING': EventStatus.UPCOMING,
    'CURRENT': EventStatus.PUBLISHED,
  });

  static EventStatus decode(String status) => _statusValues.map[status];

  static String encode(EventStatus status) => _statusValues.reverse[status];
}

You can see that it is very dificult and we have to write alot of boilerplate code to handle Enums in Dart. We can add support for constructors and params in Enum to prevent boilerplate code. For example we can have something like this:

enum EventStatus {
  const MyEnum(this.value);

  final String value;

  UPCOMING("UPCOMING"), CURRENT("CURRENT");
}

The code above compile in Java. And the code bellow compile in Kotlin:

enum class Color(val rgb: Int) {
        RED(0xFF0000),
        GREEN(0x00FF00),
        BLUE(0x0000FF)
}

What I am proposing is to have a more concise and easy way to deal with Enum in Dart.

@keertip keertip added the request Requests to resolve a particular developer problem label Sep 11, 2019
@munificent
Copy link
Member

I think #158 is essentially a duplicate of this.

@munificent munificent added the state-duplicate This issue or pull request already exists label Sep 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
request Requests to resolve a particular developer problem state-duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants