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>
145 lines
4.7 KiB
Dart
145 lines
4.7 KiB
Dart
// Copyright (c) 2013, 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.
|
|
|
|
import 'dart:mirrors';
|
|
import 'package:expect/expect.dart';
|
|
|
|
class Class<T> {
|
|
late num numField;
|
|
late bool boolField;
|
|
late String stringField;
|
|
late T tField;
|
|
late dynamic _privateField;
|
|
|
|
Class.nongeneric(this.numField);
|
|
Class.named({this.boolField = false});
|
|
Class.optPos([this.stringField = 'default']);
|
|
Class.generic(this.tField);
|
|
Class.private(this._privateField);
|
|
|
|
Class.explicitType(num this.numField);
|
|
Class.withNoType(this.numField);
|
|
Class.withSubtype(int this.numField);
|
|
}
|
|
|
|
class Constant {
|
|
final num value;
|
|
const Constant(this.value);
|
|
}
|
|
|
|
void main() {
|
|
MethodMirror mm;
|
|
ParameterMirror pm;
|
|
|
|
mm = reflectClass(Class).declarations[#Class.nongeneric] as MethodMirror;
|
|
pm = mm.parameters.single;
|
|
Expect.equals(#numField, pm.simpleName);
|
|
Expect.equals(reflectClass(num), pm.type);
|
|
Expect.isFalse(pm.isNamed);
|
|
Expect.isFalse(pm.isFinal);
|
|
Expect.isFalse(pm.isOptional);
|
|
Expect.isFalse(pm.hasDefaultValue);
|
|
Expect.isFalse(pm.isPrivate);
|
|
Expect.isFalse(pm.isStatic);
|
|
Expect.isFalse(pm.isTopLevel);
|
|
|
|
mm = reflectClass(Class).declarations[#Class.named] as MethodMirror;
|
|
pm = mm.parameters.single;
|
|
Expect.equals(#boolField, pm.simpleName);
|
|
Expect.equals(reflectClass(bool), pm.type);
|
|
Expect.isTrue(pm.isNamed);
|
|
Expect.isFalse(pm.isFinal);
|
|
Expect.isTrue(pm.isOptional);
|
|
Expect.isTrue(pm.hasDefaultValue);
|
|
Expect.equals(false, pm.defaultValue!.reflectee);
|
|
Expect.isFalse(pm.isPrivate);
|
|
Expect.isFalse(pm.isStatic);
|
|
Expect.isFalse(pm.isTopLevel);
|
|
|
|
mm = reflectClass(Class).declarations[#Class.optPos] as MethodMirror;
|
|
pm = mm.parameters.single;
|
|
Expect.equals(#stringField, pm.simpleName);
|
|
Expect.equals(reflectClass(String), pm.type);
|
|
Expect.isFalse(pm.isNamed);
|
|
Expect.isFalse(pm.isFinal);
|
|
Expect.isTrue(pm.isOptional);
|
|
Expect.isTrue(pm.hasDefaultValue);
|
|
Expect.equals('default', pm.defaultValue!.reflectee);
|
|
Expect.isFalse(pm.isPrivate);
|
|
Expect.isFalse(pm.isStatic);
|
|
Expect.isFalse(pm.isTopLevel);
|
|
|
|
mm = reflectClass(Class).declarations[#Class.generic] as MethodMirror;
|
|
pm = mm.parameters.single;
|
|
Expect.equals(#tField, pm.simpleName);
|
|
Expect.equals(reflectClass(Class).typeVariables.single, pm.type);
|
|
Expect.isFalse(pm.isNamed);
|
|
Expect.isFalse(pm.isFinal);
|
|
Expect.isFalse(pm.isOptional);
|
|
Expect.isFalse(pm.hasDefaultValue);
|
|
Expect.isFalse(pm.isPrivate);
|
|
Expect.isFalse(pm.isStatic);
|
|
Expect.isFalse(pm.isTopLevel);
|
|
|
|
mm = reflectClass(Class).declarations[#Class.private] as MethodMirror;
|
|
pm = mm.parameters.single;
|
|
Expect.equals(#_privateField, pm.simpleName);
|
|
Expect.equals(currentMirrorSystem().dynamicType, pm.type);
|
|
Expect.isFalse(pm.isNamed);
|
|
Expect.isFalse(pm.isFinal);
|
|
Expect.isFalse(pm.isOptional);
|
|
Expect.isFalse(pm.hasDefaultValue);
|
|
Expect.isTrue(pm.isPrivate);
|
|
Expect.isFalse(pm.isStatic);
|
|
Expect.isFalse(pm.isTopLevel);
|
|
|
|
mm = reflectClass(Class).declarations[#Class.explicitType] as MethodMirror;
|
|
pm = mm.parameters.single;
|
|
Expect.equals(#numField, pm.simpleName);
|
|
Expect.equals(reflectClass(num), pm.type);
|
|
Expect.isFalse(pm.isNamed);
|
|
Expect.isFalse(pm.isFinal);
|
|
Expect.isFalse(pm.isOptional);
|
|
Expect.isFalse(pm.hasDefaultValue);
|
|
Expect.isFalse(pm.isPrivate);
|
|
Expect.isFalse(pm.isStatic);
|
|
Expect.isFalse(pm.isTopLevel);
|
|
|
|
mm = reflectClass(Class).declarations[#Class.withNoType] as MethodMirror;
|
|
pm = mm.parameters.single;
|
|
Expect.equals(#numField, pm.simpleName);
|
|
Expect.equals(reflectClass(num), pm.type);
|
|
Expect.isFalse(pm.isNamed);
|
|
Expect.isFalse(pm.isFinal);
|
|
Expect.isFalse(pm.isOptional);
|
|
Expect.isFalse(pm.hasDefaultValue);
|
|
Expect.isFalse(pm.isPrivate);
|
|
Expect.isFalse(pm.isStatic);
|
|
Expect.isFalse(pm.isTopLevel);
|
|
|
|
mm = reflectClass(Class).declarations[#Class.withSubtype] as MethodMirror;
|
|
pm = mm.parameters.single;
|
|
Expect.equals(#numField, pm.simpleName);
|
|
Expect.equals(reflectClass(int), pm.type);
|
|
Expect.isFalse(pm.isNamed);
|
|
Expect.isFalse(pm.isFinal);
|
|
Expect.isFalse(pm.isOptional);
|
|
Expect.isFalse(pm.hasDefaultValue);
|
|
Expect.isFalse(pm.isPrivate);
|
|
Expect.isFalse(pm.isStatic);
|
|
Expect.isFalse(pm.isTopLevel);
|
|
|
|
mm = reflectClass(Constant).declarations[#Constant] as MethodMirror;
|
|
pm = mm.parameters.single;
|
|
Expect.equals(#value, pm.simpleName);
|
|
Expect.equals(reflectClass(num), pm.type);
|
|
Expect.isFalse(pm.isNamed);
|
|
Expect.isFalse(pm.isFinal); // N.B.
|
|
Expect.isFalse(pm.isOptional);
|
|
Expect.isFalse(pm.hasDefaultValue);
|
|
Expect.isFalse(pm.isPrivate);
|
|
Expect.isFalse(pm.isStatic);
|
|
Expect.isFalse(pm.isTopLevel);
|
|
}
|