-
Notifications
You must be signed in to change notification settings - Fork 1.7k
enums should be allowed in constant expressions #29278
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
Comments
I agree, we should extend I'm considering changing the requirement for |
Not sure when this changed, but this now works in the VM 😕 . It does not work in dart2js, and the analyzer is unhappy about it still. |
It shouldn't work. The language specification has not been changed to allow this. |
This would also be nice for writing assert statements in a const constructor for an enum class field. I was trying to do this: class ExprSymbol {
final SymbolType type;
final num value;
final int argumentCount;
const ExprSymbol(this.type, this.value, this.argumentCount)
: assert(type == SymbolType.number ? argumentCount == 0 : argumentCount != null);
} |
consider also: |
I think the original request can be closed in favor of dart-lang/language#312 in the new language repo. (That enhancement doesn't address other requests above like enum fields in const classes, or accessing an enum's index and using |
Closing as this is now being tracked in dart-lang/language#312 If you voted on the present issue, I'd encourage you to vote on issue 312 above in the language repo. |
Enums produce an error in switch expressions Case expressions must be constant |
Enums are constants. They also have primitive equality, so they should work in switches. The problem here was that they did not work with |
It's common in Flutter code to use enums heavily. Since these are always entirely known at compile time, it is unfortunate that they cannot be used in constant expressions. (Constant expressions with enums would allow us to assert things about arguments based on enums in constant constructors which would allow big performance wins in widget trees which could be constant in principle but can't today because of this issue.)
The text was updated successfully, but these errors were encountered: