Skip to content

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

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
leafpetersen opened this issue Apr 11, 2019 · 9 comments
Closed

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

leafpetersen opened this issue Apr 11, 2019 · 9 comments
Labels
state-duplicate This issue or pull request already exists

Comments

@leafpetersen
Copy link
Member

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));
}
@leafpetersen
Copy link
Member Author

cc @Hixie @mit-mit

@mpfaff
Copy link

mpfaff commented Aug 10, 2020

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

@mit-mit
Copy link
Member

mit-mit commented Aug 10, 2020

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

@Hixie
Copy link

Hixie commented Aug 10, 2020

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).

@rdnobrega
Copy link

@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...

@mit-mit
Copy link
Member

mit-mit commented Aug 28, 2020

Why wouldn't it be?

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

@rdnobrega
Copy link

@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.

eseidel added a commit to eseidel/clank that referenced this issue Apr 12, 2021
* Add support for using devices (and tested)
* Also added Tattle since with othersClank support it was simple.
* Re-ordered CardType definitions to have cost last.

I attempted to add a compile time assert for set == CardSet.starter || (skillCost > 0 || swordsCost >0), but ran into dart-lang/language#312
@eernstg
Copy link
Member

eernstg commented Aug 19, 2021

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.

@lrhn
Copy link
Member

lrhn commented Sep 29, 2021

Closing as duplicate of #1811.

@lrhn lrhn closed this as completed Sep 29, 2021
@lrhn lrhn added the state-duplicate This issue or pull request already exists label Sep 29, 2021
copybara-service bot pushed a commit to dart-lang/sdk that referenced this issue Aug 7, 2023
Confirm that const enum equality works with the addition of enum comparisons in recent language changes like dart-lang/language#312

We can keep an extra test in the suite.

Fixes #45441

Bug: #45441
Change-Id: I80f7496e172a9a09e9bec3ea1643811788b38443
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/316701
Commit-Queue: Kallen Tu <[email protected]>
Reviewed-by: Konstantin Shcheglov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state-duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

7 participants