Allow omitting 'const' from List/Map(/Constructor?) literals inside a known const context. #4803
Labels
area-language
Dart language related items (some items might be better tracked at github.com/dart-lang/language).
closed-duplicate
Closed in favor of an existing report
type-enhancement
A request for a change that isn't a bug
To write a const list of lists, you need to prefix every constant list with 'const', e.g.:
const [const [1,2], const [3,4], const [5,6]]
The inner 'const' are really superfluous, since it would currently be an error to omit them.
We could allow omitting these extra 'const' markers and automatically assume that any List or Map literal inside a const literal is itself const. That is, allow:
const [[1,2], [3,4], [5,6]]
to mean the same thing as the above.
This would:
a) make it much easier to write larger const structures by hand, and
b) make it easier to transform a JSON value to a const (just add 'const' in front, and escape '$' in strings)
Optionally, we can also allow removing 'const' in front of 'const Foo(a,b)' inside a const literal. This would match the missing 'const' in front of meta-data annotation, where we allow it to be omitted because we know it's a const-context.
We could even make the initial const optional in a known const context, so it's just "static const List foo = [1,2,3]" ... but that's probably a more serious change, since it can more easily hide bugs.
The text was updated successfully, but these errors were encountered: