Skip to content

Make const / new optional #18241

Closed
Closed
@DartBot

Description

@DartBot

This issue was originally filed by @seaneagan


issue #4046 is about making the "const" / "new" modifier optional within const expressions, defaulting it to "const".

This could be generalized to make "const" / "new" optional everywhere, for both consistency and to add the following capabilities:

In const constructor initializers, leaving off "const" would allow the constness to be determined by the constructor call site, which would mean you could call other const constructors for example.

It would allow for the existence of const functions, and const factory constructors. And finally, leaving off "const" / "new" from any other call sites, would default it to "new", which would make it no longer necessary to expose top-level functions (extra API surface, lack of generic type support) whose sole purpose is to allow the call site to omit "new " when creating an instance

Example:

class Foo {
  final int x;
  const Foo(this.x);
  const factory Foo.bar(x) => Bar(x, []);
}

class Bar extends Foo {
  final List list;
  const Bar(int x, this.list) : super(x);
}

const foo(x) => Foo(x);

Then the following equivalencies would hold:

a) const foo(x) -> const Foo(x)
b) foo(x) -> new Foo(x)
c) const Foo(x) -> const Bar(x, const [])
d) new Foo(x) -> new Bar(x, [])
e) Foo(x) -> foo(x)

Examples const functions would be the top-level functions in dart:math. And package:matchers could expose matchers as const constructors instead of top-level methods, which would allow them to be used in both annotations, and test code, without needing "new" / "const" in either case, and the matchers would then also be able to support generic type arguments. See:

https://groups.google.com/a/dartlang.org/forum/#!topic/misc/0_DXEjQSoiU

Metadata

Metadata

Assignees

Labels

area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).closed-not-plannedClosed as we don't intend to take action on the reported issuetype-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