Skip to content

Make enhanced enum fields available in constant contexts #4587

@FMorschel

Description

@FMorschel

In one of my projects, I use enhanced enums to create constants. Some of these should work together with one another. And I have a String field that should have the same value between them. Currently, this must be defined on a private constant and used to instantiate the enum constants.

We can't even write asserts to make sure they match because accessing these fields in a const constructor is invalid.


Since, currently, all enhanced enum fields must be constant, we could think of considering all of them potentially constant (I think that would solve the issue), right? So we could do:

enum One {
  a('string a'),
  b('bbbbb');

  const One(this.value);
  final String value;
}

enum Two {
  a(.a),
  b(.b);

  const Two(One one) : value = one.value;
  final String value;
}

Is there a better/another solution? Are there any problems with my suggestion? Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    requestRequests to resolve a particular developer problem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions