Skip to content

Record fields can't be used as const? #51895

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
mdebbar opened this issue Mar 29, 2023 · 1 comment
Closed

Record fields can't be used as const? #51895

mdebbar opened this issue Mar 29, 2023 · 1 comment
Labels
legacy-area-analyzer Use area-devexp instead.

Comments

@mdebbar
Copy link
Contributor

mdebbar commented Mar 29, 2023

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?

@eernstg
Copy link
Member

eernstg commented 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 of String (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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
legacy-area-analyzer Use area-devexp instead.
Projects
None yet
Development

No branches or pull requests

3 participants