Skip to content

Allow == on Enum values in const expressions. #312

Closed

Description

@leafpetersen

As reported in:

The specification does not allow == to be used on enum values in const contexts. This seems reasonable to allow, customers have asked for it, and it should be an easy change to make.

Example from dart-lang/sdk#26980

enum MyEnum { a, b }

class Test {
  const Test(MyEnum arg) : value = arg == MyEnum.a ? 0 : 1;
  final int value;
}

void main() {
  Test q = const Test(MyEnum.a);
  Test r = const Test(MyEnum.a);
  print(identical(q, r));
}

Activity

  1. leafpetersen commented on Apr 11, 2019

    @leafpetersen
    MemberAuthor
  2. mpfaff commented on Aug 10, 2020

    @mpfaff

    Is anything being done about this? It seems like a pretty basic language feature, especially considering how weak Dart's enums already are.

  3. mit-mit commented on Aug 10, 2020

    @mit-mit
    Member

    @Hixie looks like you opened dart-lang/sdk#29278 originally; do you still believe this would be a solid improvement?

  4. Hixie commented on Aug 10, 2020

    @Hixie

    I think this is table stakes. I think we should go much, much further and allow all code in const expressions except code that ends up having side-effects (e.g. opening sockets, printing to stdout, accessing the filesystem, that kind of thing).

  5. rdnobrega commented on Aug 27, 2020

    @rdnobrega

    @mit-mit Why wouldn't it be? Enums are already constant-like structures, and using static class members is just a lot of boilerplate code for such a simple thing...

  6. mit-mit commented on Aug 28, 2020

    @mit-mit
    Member

    Why wouldn't it be?

    I'm not saying it isn't, just asking for input.

  7. rdnobrega commented on Aug 28, 2020

    @rdnobrega

    @mit-mit Hi! I'd like to say what I think and am trying to do and would like your feedback on the topic. I have a list of type of working hours, being 4: hflex, hnor, hnot, henor. Today we can write with a class for const expressions:

    class WorkType { final int _value; static const HFLEX = WorkType._internal(0); static const HNUCL = WorkType._internal(1); static const HENOR = WorkType._internal(2); static const HENOT = WorkType._internal(3); const WorkType._internal(this._value); }

    with enums, I'd like to write like this:
    enum WorkType { HFLEX, HNUCL, HENOR, HENOT }

    Furthermore, I'd like enums to be even more flexible, first with value setting for ints only:
    enum WorkType { HFLEX: 0, HNUCL: 1, HENOR: 2, HENOT: 3}

    and being even more flexible, enumeration with another enums:
    enum WorkType { HFLEX, HNUCL, HENOR, HENOT }
    enum WorkGroup<WorkType> { Group1: WorkType.HNUCL + WorkType.HENOR + WorkType.HENOT }

    This would allow loop in WorkType.values() without the concern of getting groups instead of single enum values.

    This would help A LOT coding flags. Of course, all of this is already possible with classes, but it is a lot more code and doesn't really quite match.

  8. added a commit that references this issue on Apr 12, 2021
  9. eernstg commented on Aug 19, 2021

    @eernstg
    Member

    This feature could be expected to be available in Dart already: An instance of an enum class has primitive equality, and that is the property that we otherwise require when constant evaluation needs to evaluated ==. So it's a surprising exception that operator == in constant expressions isn't specified using the same concept. Cf. #1811 where this generalization is proposed.

  10. lrhn commented on Sep 29, 2021

    @lrhn
    Member

    Closing as duplicate of #1811.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    state-duplicateThis issue or pull request already exists

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions