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
Analyzer incorrectly reports "In constant expressions, operands of this operator must be of type 'bool', 'num', 'String' or 'null'" for == operator with enum operand #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));
}
VM prints "true", showing that the const expression is fine.
Analyzer says:
[error] In constant expressions, operands of this operator must be of type 'bool', 'num', 'String' or 'null' (test.dart, line 4, col 36)
The language spec says that a constant expression can be (among other things):
An expression of one of the forms e1 == e2 or e1 != e2 where e1 and e2 are constant expressions that evaluate to a numeric, string or boolean value or to null.
It doesn't whitelist enum values. Actually, it doesn't allow enum constants anywhere in constant expressions. I'm guessing this was an oversight.
We could allow any compile-time constant value of a class that doesn't override Object.== as the left-hand side of a constant == operation, but that's probably a little too clever to be actually useful (that would make it a breaking change to add operator== to a class with a const constructor, which it isn't now).
Allowing enums is probably fine and useful.
(The spec, by definition, cannot have bugs - only "features" :)
After reading #30288 (comment) - it seems like I just need to use identical instead of == and it's all fine. While that makes some sense, it seems like it should be possible to interpret opt1 == null as being identical to identical(opt1, null) (or at least hint the user that that would work here).
Apparently, the implementation of the added support for comparison with null in constant expressions was never requested explicitly. I just created #33408 to make that happen. I'm closing this because the 'area-language' part was resolved already in Oct 2017 with commit f11f2ed.
Oops, I was looking at this comment and concluding that this issue was about comparison with null. It actually started out as an issue about comparison of enum values, and that has not yet been addressed: Reopening.
Activity
bwilkerson commentedon Jul 28, 2016
Or showing that the VM has a bug :-)
The language spec says that a constant expression can be (among other things):
An expression of one of the forms e1 == e2 or e1 != e2 where e1 and e2 are constant expressions that evaluate to a numeric, string or boolean value or to null.
It doesn't whitelist enum values. Actually, it doesn't allow enum constants anywhere in constant expressions. I'm guessing this was an oversight.
Hixie commentedon Jul 28, 2016
I think if there's a bug, it's in the spec and the analyzer, not in the VM. :-)
lrhn commentedon Jul 29, 2016
We could allow any compile-time constant value of a class that doesn't override
Object.==as the left-hand side of a constant==operation, but that's probably a little too clever to be actually useful (that would make it a breaking change to addoperator==to a class with a const constructor, which it isn't now).Allowing enums is probably fine and useful.
(The spec, by definition, cannot have bugs - only "features" :)
Add FlutterLogoDecoration tests (#10209)
dnfield commentedon Jun 11, 2018
It seems like the following should be possible (or at least it seems to make sense), but it is not as a result of this:
The goal being to have a const constructor where only one of two parameters is ever specified.
dnfield commentedon Jun 11, 2018
After reading #30288 (comment) - it seems like I just need to use
identicalinstead of==and it's all fine. While that makes some sense, it seems like it should be possible to interpretopt1 == nullas being identical toidentical(opt1, null)(or at least hint the user that that would work here).eernstg commentedon Jun 11, 2018
Apparently, the implementation of the added support for comparison with null in constant expressions was never requested explicitly. I just created #33408 to make that happen. I'm closing this because the 'area-language' part was resolved already in Oct 2017 with commit f11f2ed.
eernstg commentedon Jun 11, 2018
Oops, I was looking at this comment and concluding that this issue was about comparison with
null. It actually started out as an issue about comparison of enum values, and that has not yet been addressed: Reopening.dnfield commentedon Jun 11, 2018
Yes, sorry I got confused between the two issues as well.
a-siva commentedon Apr 11, 2019
Flutter code has been fixed and a language issue has been created allowing '==' on Enum values, closing this.
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…