Allow const instance fields as const expressions on const objects. #16547
Labels
area-language
Dart language related items (some items might be better tracked at github.com/dart-lang/language).
type-enhancement
A request for a change that isn't a bug
Uh oh!
There was an error while loading. Please reload this page.
We currently disallow
const
on instance fields because we don't have a good semantics for them.Consider allowing
The instance const field will act just as a final field, except when initialized by a const invocation of a const constructor (i.e., except when on an actual compile-time constant).
Then we can allow expressions like
x.foo
to be compile time constant expressions ifx
is a compile time constant expression with a value that has a const instance field namedfoo
.This will allow changing a static-only class to an object, and still expose the same compile time constants.
Other uses could be an enum implementation that assigns a unique id to each enum object, and then allowing switching on the id instead of on the object by writing
case FOO.id:
.This breaks the field/getter symmetry, so perhaps we can also allow:
where the expression must be a compile time constant expression potentially containing
this.x
references to const fields (just as potential const expressions in constructors can refer to const constructor parameters).The text was updated successfully, but these errors were encountered: