You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow == on Enum values in const expressions. #312
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.
enumMyEnum { a, b }
classTest {
constTest(MyEnum arg) : value = arg ==MyEnum.a ?0:1;
finalint value;
}
voidmain() {
Test q =constTest(MyEnum.a);
Test r =constTest(MyEnum.a);
print(identical(q, r));
}
Reacted by Bob Nystrom, Patryk Ludwikowski, Rolly Peres, Vlad, Alexandr Priezzhev, Greg Spencer, Hugo Delaunay, Wyte Krongapiradee, Fred Moolekamp, Michael Pfaff and 15 more
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).
Reacted by Michael Thomsen, Alexandre Ardhuin, Tong Mu and Wdestroier
@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 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.
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.
Activity
leafpetersen commentedon Apr 11, 2019
cc @Hixie @mit-mit
mpfaff commentedon 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 commentedon Aug 10, 2020
@Hixie looks like you opened dart-lang/sdk#29278 originally; do you still believe this would be a solid improvement?
Hixie commentedon 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 commentedon Aug 27, 2020
@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 commentedon Aug 28, 2020
I'm not saying it isn't, just asking for input.
rdnobrega commentedon Aug 28, 2020
@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.
Add support for devices
eernstg commentedon Aug 19, 2021
This feature could be expected to be available in Dart already: An instance of an
enumclass has primitive equality, and that is the property that we otherwise require when constant evaluation needs to evaluated==. So it's a surprising exception thatoperator ==in constant expressions isn't specified using the same concept. Cf. #1811 where this generalization is proposed.lrhn commentedon Sep 29, 2021
Closing as duplicate of #1811.
[analyzer] Add test for enum equality in constant evaluation.
Fix enum instances comparison to use `equality ==` since dart has the…
Replace more enum instances comparison to use `equality ==` instead o…
`[cupertino_ui]` replace identical checks with equality when dealing …
`[cupertino_ui]` replace identical checks with equality when dealing …
`[material_ui]` replace identical checks with equality when dealing w…
`[cupertino_ui]` replace identical checks with equality when dealing …
`[material_ui]` replace identical checks with equality when dealing w…
Update more enum instances comparison to use equality == since dart h…