8bfb683892
This CL enables the primary constructors feature by default in Dart 3.13.
The primary constructors feature is a brevity feature. There are no new semantics, but it allows us to express declarations in a less verbose way.
This feature allows one constructor and a set of instance variables to be specified in the header of a declaration.
Currently a declaration with a constructor and some fields is written as:
```dart
// Current syntax.
class Point {
int x;
int y;
Point(this.x, this.y);
}
```
With a primary constructor, we would write the above as:
```
class Point(var int x, var int y);
```
If a primary constructor needs an initializer list or a body, they can be
specified inside the class using the `this` body syntax:
```dart
class Point(var int x, var int y) {
this : assert(x >= 0) {
print('Point created at $x, $y');
}
}
```
As part of this feature, you can also use the `new` and `factory` keywords to
declare constructors in the class body without repeating the class name:
```dart
class Point {
int x, y;
// Equivalent to Point(this.x, this.y)
new(this.x, this.y);
// Equivalent to Point.origin()
new origin() : x = 0, y = 0;
// Equivalent to factory Point.clone(Point other)
factory clone(Point other) => Point(other.x, other.y);
}
```
To learn more about the feature, check out the feature specification located here: https://github.com/dart-lang/language/blob/main/accepted/future-releases/primary-constructors/feature-specification.md
Tested: Has existing language, CFE, analyzer, analysis server tests.
Bug: https://github.com/dart-lang/sdk/issues/61524
Change-Id: I296f2fcd918b87bf2a1dd00256340759866c2423
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/489241
Reviewed-by: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Nicholas Shahan <nshahan@google.com>
Reviewed-by: Michael Thomsen <mit@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
Reviewed-by: Ben Konyi <bkonyi@google.com>
67 lines
2.0 KiB
C++
67 lines
2.0 KiB
C++
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
// NOTE: THIS FILE IS GENERATED. DO NOT EDIT.
|
|
//
|
|
// Instead modify 'tools/experimental_features.yaml' and run
|
|
// 'dart tools/generate_experimental_flags.dart' to update.
|
|
|
|
#include "vm/experimental_features.h"
|
|
|
|
#include <cstring>
|
|
#include "platform/assert.h"
|
|
#include "vm/globals.h"
|
|
|
|
namespace dart {
|
|
|
|
bool GetExperimentalFeatureDefault(ExperimentalFeature feature) {
|
|
constexpr bool kFeatureValues[] = {
|
|
true, true, true, true, true, true, true, true, true, true, true,
|
|
true, true, true, true, true, true, true, true, true, true, true,
|
|
true, true, true, true, true, true, true, true, true, true,
|
|
};
|
|
ASSERT(static_cast<size_t>(feature) < ARRAY_SIZE(kFeatureValues));
|
|
return kFeatureValues[static_cast<int>(feature)];
|
|
}
|
|
|
|
const char* GetExperimentalFeatureName(ExperimentalFeature feature) {
|
|
constexpr const char* kFeatureNames[] = {
|
|
"primary-constructors",
|
|
"private-named-parameters",
|
|
"dot-shorthands",
|
|
"native-assets",
|
|
"getter-setter-error",
|
|
"sound-flow-analysis",
|
|
"null-aware-elements",
|
|
"inference-using-bounds",
|
|
"wildcard-variables",
|
|
"digit-separators",
|
|
"inference-update-3",
|
|
"inline-class",
|
|
"inference-update-2",
|
|
"sealed-class",
|
|
"class-modifiers",
|
|
"records",
|
|
"patterns",
|
|
"unnamed-libraries",
|
|
"inference-update-1",
|
|
"enhanced-enums",
|
|
"named-arguments-anywhere",
|
|
"super-parameters",
|
|
"constructor-tearoffs",
|
|
"generic-metadata",
|
|
"triple-shift",
|
|
"nonfunction-type-aliases",
|
|
"non-nullable",
|
|
"extension-methods",
|
|
"constant-update-2018",
|
|
"control-flow-collections",
|
|
"set-literals",
|
|
"spread-collections",
|
|
};
|
|
ASSERT(static_cast<size_t>(feature) < ARRAY_SIZE(kFeatureNames));
|
|
return kFeatureNames[static_cast<int>(feature)];
|
|
}
|
|
|
|
} // namespace dart
|