Skip to content

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

Closed

Description

@alan-knight

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.

Activity

  1. madsager commented on Jul 11, 2012

    @madsager
    Contributor

    Added Area-Language, Triaged labels.

  2. DartBot commented on Jul 11, 2012

    @DartBot

    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())

  3. lrhn commented on Jul 11, 2012

    @lrhn
    Member

    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.

  4. DartBot commented on Jul 11, 2012

    @DartBot

    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.

  5. DartBot commented on Jul 11, 2012

    @DartBot

    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.

  6. gbracha commented on Jul 11, 2012

    @gbracha
    Contributor

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

  7. gbracha commented on Jul 12, 2012

    @gbracha
    Contributor

    Issue #3974 has been merged into this issue.


    cc @sigmundch.

  8. lrhn commented on Sep 3, 2012

    @lrhn
    Member

    Issue #4803 has been merged into this issue.

  9. justinfagnani commented on Aug 29, 2013

    @justinfagnani
    Contributor

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

  10. DartBot commented on Dec 2, 2013

    @DartBot

    This comment was originally written by @seaneagan


    Example user confusion here:

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

  11. kasperl commented on Jul 10, 2014

    @kasperl

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

  12. lrhn commented on Jul 15, 2014

    @lrhn
    Member

    Issue #20021 has been merged into this issue.

  13. kasperl commented on Aug 4, 2014

    @kasperl
  14. zoechi commented on Oct 19, 2015

    @zoechi
    Contributor

    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

  15. added
    P2A bug or feature request we're likely to work on
    type-enhancementA request for a change that isn't a bug
    and removed on Feb 29, 2016
  16. eernstg commented on Mar 5, 2018

    @eernstg
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2A bug or feature request we're likely to work onarea-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).type-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions