Skip to content
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

const at the top level of a structure should propagate down #4046

Closed
alan-knight opened this issue Jul 10, 2012 · 15 comments
Closed

const at the top level of a structure should propagate down #4046

alan-knight opened this issue Jul 10, 2012 · 15 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@alan-knight
Copy link
Contributor

I have some large objects which are constant. As it stands, writing one of these out requires 22 occurrences of the word "const", one for every field (which isn't a string literal or number, because those are implicitly const and it's an error to write it explicitly).

It is not legal to write a const constructor call, literal Map or literal List containing non-const expressions. So it seems unnecessary to require a const annotation on the individual pieces. They are required to be const, so we should just enforce that they are without requiring the developer to also spell it out. That is to say, instead of having to write

var list = const [ const ['a'], const ['b'], const ['c']];

I should be able to write

var list = const [ ['a'], ['b'], ['c']];

there the outer const also applies to the inner lists. And the same for maps and const constructor invocations.

@madsager
Copy link
Contributor

Added Area-Language, Triaged labels.

@DartBot
Copy link

DartBot commented Jul 11, 2012

This comment was originally written by @seaneagan


I really like this. The main problem is what do you use instead of "const" for the nested constructor calls? Using "new" wouldn't look right. Using nothing is better, but then it isn't consistent with constructor calls elsewhere. I think this is yet another reason to remove the "new" keyword (and factory constructors as they would no longer be needed).

This would really help out with making annotations more terse and readable:

@SomeAnnotation(const Foo(const Bar(), const Baz())

@SomeAnnotation(Foo(Bar(), Baz())

@lrhn
Copy link
Member

lrhn commented Jul 11, 2012

You can change const expressions without changing non-const constructor calls.
E.g., make "const" in expression context not be an construct similar to "new", but rather a marker that makes the following expression be interpreted in a slightly different way: all maps and lists are constant, all function calls are const constructor invocations, and only some operators are allowed.
It would amount to the same as the suggestion, but const expressions would then not have to use the same grammar as non-const expressions.
And metadata annotations would simply use the const expression syntax by default.

@DartBot
Copy link

DartBot commented Jul 11, 2012

This comment was originally written by @seaneagan


What I meant to say is that bare (without "new" or "const") constructor calls would look weird inside these proposed "const" expressions... unless they were the norm in the language, as would be the case if "new" were removed.

@DartBot
Copy link

DartBot commented Jul 11, 2012

This comment was originally written by @seaneagan


Then there would be no special syntax needed inside of "const" expressions, it would just implicitly apply "const" modifiers to everything.

@gbracha
Copy link
Contributor

gbracha commented Jul 11, 2012

Set owner to @gbracha.
Added this to the Later milestone.
Removed Type-Defect label.
Added Type-Enhancement, Accepted labels.

@gbracha
Copy link
Contributor

gbracha commented Jul 12, 2012

Issue #3974 has been merged into this issue.


cc @sigmundch.

@lrhn
Copy link
Member

lrhn commented Sep 3, 2012

Issue #4803 has been merged into this issue.

@justinfagnani
Copy link
Contributor

This would be great in metadata annotations where we don't require const for the constructor, but then require it for arguments.

@DartBot
Copy link

DartBot commented Dec 2, 2013

This comment was originally written by @seaneagan


Example user confusion here:

http://stackoverflow.com/questions/20223354/custom-metadata-with-unknown-parameter-length

@kasperl
Copy link

kasperl commented Jul 10, 2014

Removed this from the Later milestone.
Added Oldschool-Milestone-Later label.

@lrhn
Copy link
Member

lrhn commented Jul 15, 2014

Issue #20021 has been merged into this issue.

@kasperl
Copy link

kasperl commented Aug 4, 2014

Removed Oldschool-Milestone-Later label.

@zoechi
Copy link
Contributor

zoechi commented Oct 19, 2015

I guess the link provides more value here than in #24620. I was sure I have seen the proposal but wasn't able to find it.
https://github.com/lrhn/dep-const/blob/master/proposal.md

@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug and removed accepted labels Feb 29, 2016
@floitschG floitschG mentioned this issue Aug 25, 2016
16 tasks
@eernstg
Copy link
Member

eernstg commented Mar 5, 2018

This feature is now an accepted part of Dart 2. The feature specification implicit-creation.md gives the details, but the following is a good starting point:

For instance creation expressions we have chosen to use const whenever possible,
and otherwise new.
...
For composite literals we have chosen to implicitly introduce const whenever it is
required by the context.

So C(42) is a constant expression if the relevant C constructor is const (even in cases where the context does not require a constant expression), and const xs = []; makes [] a constant list, but var xs = []; makes [] a non-constant list.

@eernstg eernstg closed this as completed Mar 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

10 participants