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>
Linter for Dart
The Dart Linter package defines lint rules that identify and report on "lints" found in Dart code. Linting is performed by the Dart
analysis server and the dart analyze command in the Dart command-line tool.
Installing
The linter is bundled with the Dart SDK; if you have an updated Dart SDK already, you're done!
Usage
The linter gives you feedback to help you catch potential errors and keep your code in line with the published
Dart Style Guide. Enforceable lint rules (or "lints") are cataloged here and can be configured via an
analysis options file. The linter is run from within the dart analyze command-line tool shipped with the
Dart SDK. Assuming you have lints configured in an analysis_options.yaml file at the root of your project with these contents:
linter:
rules:
- annotate_overrides
- hash_and_equals
- prefer_is_not_empty
you could lint your package like this:
$ dart analyze .
and see any violations of the annotate_overrides, hash_and_equals, and prefer_is_not_empty rules in the console.
To help you choose the rules you want to enable for your package, we have provided a complete list of rules
with lints recommended by the Dart team collected in package:lints. Lints recommended for Flutter apps, packages,
and plugins are documented in package:flutter_lints.
If a specific lint warning should be ignored, it can be flagged with a comment. For example,
// ignore: camel_case_types
class whyOhWhy { }
tells the Dart analyzer to ignore this instance of the camel_case_types warning.
End-of-line comments are supported as well. The following communicates the same thing:
class whyOhWhy { // ignore: camel_case_types
To ignore a rule for an entire file, use the ignore_for_file comment flag. For example,
// ignore_for_file: camel_case_types
...
class whyOhWhy { }
tells the Dart analyzer to ignore all occurrences of the camel_case_types warning in this file.
As lints are treated the same as errors and warnings by the analyzer, their severity can similarly be configured in an options file. For example, an analysis options file that specifies
linter:
rules:
- camel_case_types
analyzer:
errors:
camel_case_types: error
tells the analyzer to treat camel_case_types lints as errors. For more on configuring analysis see the analysis option file docs.
Contributing
Feedback is greatly appreciated and contributions are welcome! Please read the contribution guidelines; mechanics of writing lints are covered here.
Features and bugs
Please file feature requests and bugs in the issue tracker.