Closed
Description
In the following code snippet, info.age
is a const value (the entire info
record is const). But the analyzer isn't happy with it.
const info = (name: 'John', age: 20);
String getNameByAge(int age) {
switch (age) {
case info.age: return info.name;
default: 'unkown';
}
}
I get the following error about case info.age
:
The expression of a constant pattern must be a valid constant.
Try making the expression a valid constant.
Is this working as intended or a bug?
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
eernstg commentedon Mar 30, 2023
This is working as intended: The only getter which can be invoked as part of constant expression evaluation is
length
, and that is only allowed when the receiver is an instance ofString
(that's the actual value of the constant expression, not the static type).Check out https://github.com/dart-lang/language/issues?q=is%3Aopen+is%3Aissue+label%3Aenhanced-const for issues about generalizations of the constant expression sublanguage in general, and, e.g., dart-lang/language#2219, where several new constant expression forms are proposed.