dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.22k stars 1.57k forks source link

Optional `new` and `const` for Dart 2 (meta issue) #30921

Open lrhn opened 7 years ago

lrhn commented 7 years ago

Dart 2 will make new and const operators optional in some cases. The feature is described in https://github.com/dart-lang/sdk/blob/master/docs/language/informal/implicit-creation.md.

This feature needs support from, at least, the common front-end. The kernel compiler can, and likely will, generate the same kernel code whether an optional new or const is present or not, so back-ends should be unaffected. Effectively, the kernel (or even the parser) can perform automatic "new/const" insertion on the program, as soon as it can recognize an invocation as referring to a constructor rather than a static method.

The feature extends the grammar with syntax that wasn't previously valid, so the formatter likely needs to be prepared for it. The analyzer may need to be prepared for the different syntax as well.

We will not back-port this feature to non-kernel based backends.

bwilkerson commented 6 years ago

@munificent I don't think there's any work for the formatter, but it would be good to double check that.

eernstg commented 6 years ago

About the need to adjust the formatter, and possibly other tools: The grammar had to be extended with support for a new construct, namely

constructorInvocation ::=
    typeName typeArguments '.' identifier arguments

and that construct is then used in a couple of places. The point is that we can now have things like C<int>.someName(42), and the ability to have actual type arguments after the class name is new. So every tool that has a parser which hasn't already been updated to handle this will need an update.

bwilkerson commented 6 years ago

Yep, but the formatter uses the analyzer parser, so I think it's probably fine.

munificent commented 6 years ago

Turns out there is one case the formatter needs to handle:

SomeGenericClass<int>.someNameConstructor();

I added support for that in https://github.com/dart-lang/dart_style/commit/b89b572673c30d6124d61bda69710ac4d25c2e56.

dgrove commented 6 years ago

Actually, since this is the meta issue that has the spec as the long pole, moving to the Dart2 Stable milestone.

leafpetersen commented 6 years ago

The formal spec bug is the only remaining issue, so closing this out.

stevenroose commented 6 years ago

Should this also work for factory constructors? I'm getting

NoSuchMethodError: No static method 'from' declared in class 'BigInt'.

when calling BigInt.from(5), f.e.

BigInt.from is an external factory constructor.

lrhn commented 6 years ago

It should work for all constructors. How are you running the code?

stevenroose commented 6 years ago

@lrhn I have Dart v2 installed at /opt/dart-v2 and I run /opt/dart-v2/bin/pub run test.

I could be that pub test runs the dart executable in PATH instead of the one in the same directory.. Can I pass to pub which Dart executable to use?

zoechi commented 6 years ago

@stevenroose perhaps https://github.com/dart-lang/pub/issues/1807

leafpetersen commented 6 years ago

Re-opening to track Angular implementation + communication and validation.

matanlurey commented 6 years ago

Good news. AngularDart has verified, mostly thanks to @MichaelRFairhurst, that optional new/const works at the latest dev SDKs, including in metadata annotations. Here is a sample PR where we removed it from all example applications: https://github.com/dart-lang/angular/pull/1408.

jodinathan commented 6 years ago

great news indeed!

Atenciosamente,

Jonathan Rezende

Em 14 de jun de 2018, à(s) 23:59, Matan Lurey notifications@github.com escreveu:

Good news. AngularDart has verified, mostly thanks to @MichaelRFairhurst https://github.com/MichaelRFairhurst, that optional new/const works at the latest dev SDKs, including in metadata annotations. Here is a sample PR where we removed it from all example applications: dart-lang/angular#1408 https://github.com/dart-lang/angular/pull/1408.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/dart-lang/sdk/issues/30921#issuecomment-397499124, or mute the thread https://github.com/notifications/unsubscribe-auth/AF6Y3j2vYdc7pCbOXK-c1eKgRx-9YJKEks5t8yMegaJpZM4PnE6T.

dgrove commented 6 years ago

@kwalrath is the docs part of this complete?

kwalrath commented 6 years ago

It depends on what you mean by "complete". The language tour has a note that new is optional.

~I'm working on a PR to improve on that (making optional new the default in the tour, and adding an example for optional const).~ In the language tour, optional new is (as of dart-lang/site-www#940) the default, and there's a new example for optional const: www.dartlang.org/guides/language/language-tour#using-constructors

For the rest of the docs and examples, we're planning to wait until dartfmt --fix is out.

Tracking issue: dart-lang/site-www#870

leafpetersen commented 6 years ago

Nothing left here is blocking for Dart 2 here, I think. As soon as dartfmt --fix is available for general use, we should announce this, and encourage people to migrate.

@munificent should we consider adding this to the dartfmt presubmit for the SDK now to start migrating and shaking out bugs? We probably really don't want to do that for tests though, so we'd need some way to exclude them. So maybe better to just start manually converting SDK code?

munificent commented 6 years ago

So maybe better to just start manually converting SDK code?

Yeah, I think running --fix manually is a better first step.

natebosch commented 6 years ago

Does the Dart VM read SDK code when you run with dart --no-preview-dart-2?

Does dart2js read SDK code when you compile with dart2js --no-preview-dart-2?

If so, dropping new and const from the SDK is blocked on removing those flags.

sigmundch commented 6 years ago

Does dart2js read SDK code when you compile with dart2js --no-preview-dart-2?

dart2js does - we use the CFE even in Dart1 mode, and the CFE will parse with the missing new/consts always (it is not hidden under a flag).

mit-mit commented 6 years ago

@lrhn what remains before we can close this as completed?

lrhn commented 6 years ago

SDK and specification still missing. I guess communication is part of communicating Dart 2.0.

srawlins commented 5 years ago

Should "Remove new and optional const from SDK code" still be part of this task? This meta issue was a planned feature for Dart 2.0, which has completed. AFAIK, no one intends to do this task any time soon. Especially with all of the work being done on the SDK libraries; this would cause a lot of churn if it landed any time in the next few months.

Close this meta issue?

Stargator commented 5 years ago

@srawlins I would say keep it here as a means of tracking it. That doesn't mean it has to be prioritized higher than the current work on the SDK libraries.