Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorder constructors in ‘Effective Dart: Usage’ and ’Constructors’ examples #5452

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions examples/misc/lib/effective_dart/usage_bad.dart
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,14 @@ void miscDeclAnalyzedButNotTested() {

// #docregion this-dot-constructor
class ShadeOfGray {
final int brightness;

ShadeOfGray(int val) : brightness = val;

ShadeOfGray.black() : this(0);

// This won't parse or compile!
// ShadeOfGray.alsoBlack() : black();

final int brightness;
}
// #enddocregion this-dot-constructor

Expand Down Expand Up @@ -330,10 +330,10 @@ Item? bestDeal(List<Item> cart) {

// #docregion shadow-nullable-field
class UploadException {
final Response? response;

UploadException([this.response]);

final Response? response;

@override
String toString() {
if (response != null) {
Expand All @@ -350,21 +350,25 @@ class UploadException {

// #docregion calc-vs-store1
class Circle1 {
double radius;
double area;
double circumference;

Circle1(double radius)
: radius = radius,
area = pi * radius * radius,
circumference = pi * 2.0 * radius;

double radius;
double area;
double circumference;
}
// #enddocregion calc-vs-store1

//----------------------------------------------------------------------------

// #docregion calc-vs-store2
class Circle2 {
Circle2(this._radius) {
_recalculate();
}

double _radius;
double get radius => _radius;
set radius(double value) {
Expand All @@ -378,10 +382,6 @@ class Circle2 {
double _circumference = 0.0;
double get circumference => _circumference;

Circle2(this._radius) {
_recalculate();
}

void _recalculate() {
_area = pi * _radius * _radius;
_circumference = pi * 2.0 * _radius;
Expand Down Expand Up @@ -430,36 +430,38 @@ class Box2 {

// #docregion field-init-at-decl
class ProfileMark {
final String name;
final DateTime start;

ProfileMark(this.name) : start = DateTime.now();
ProfileMark.unnamed()
: name = '',
start = DateTime.now();

final String name;
final DateTime start;
}
// #enddocregion field-init-at-decl

//----------------------------------------------------------------------------

// #docregion field-init-as-param
class Point0 {
double x, y;
Point0(double x, double y)
: x = x,
y = y;

double x, y;
}
// #enddocregion field-init-as-param

//----------------------------------------------------------------------------

// #docregion late-init-list
class Point1 {
late double x, y;
Point1.polar(double theta, double radius) {
x = cos(theta) * radius;
y = sin(theta) * radius;
}

late double x, y;
}
// #enddocregion late-init-list

Expand Down
30 changes: 16 additions & 14 deletions examples/misc/lib/effective_dart/usage_good.dart
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,10 @@ class Response {

// #docregion shadow-nullable-field
class UploadException {
final Response? response;

UploadException([this.response]);

final Response? response;

@override
String toString() {
final response = this.response;
Expand All @@ -380,10 +380,10 @@ class UploadException {

// #docregion calc-vs-store
class Circle {
double radius;

Circle(this.radius);

double radius;

double get area => pi * radius * radius;
double get circumference => pi * 2.0 * radius;
}
Expand Down Expand Up @@ -466,14 +466,14 @@ class Box2 {

// #docregion this-dot-constructor
class ShadeOfGray {
final int brightness;

ShadeOfGray(int val) : brightness = val;

ShadeOfGray.black() : this(0);

// But now it will!
ShadeOfGray.alsoBlack() : this.black();

final int brightness;
}
// #enddocregion this-dot-constructor

Expand All @@ -485,11 +485,11 @@ class BaseBox {

// #docregion param-dont-shadow-field-ctr-init
class Box3 extends BaseBox {
Object? value;

Box3(Object? value)
: value = value,
super(value);

Object? value;
}
// #enddocregion param-dont-shadow-field-ctr-init

Expand All @@ -499,40 +499,42 @@ class Document {}

// #docregion field-init-at-decl
class ProfileMark {
final String name;
final DateTime start = DateTime.now();

ProfileMark(this.name);
ProfileMark.unnamed() : name = '';

final String name;
final DateTime start = DateTime.now();
}
// #enddocregion field-init-at-decl

//----------------------------------------------------------------------------

// #docregion field-init-as-param
class Point0 {
double x, y;
Point0(this.x, this.y);

double x, y;
}
// #enddocregion field-init-as-param

//----------------------------------------------------------------------------

// #docregion late-init-list
class Point1 {
double x, y;
Point1.polar(double theta, double radius)
: x = cos(theta) * radius,
y = sin(theta) * radius;

double x, y;
}
// #enddocregion late-init-list

//----------------------------------------------------------------------------

// #docregion semicolon-for-empty-body
class Point2 {
double x, y;
Point2(this.x, this.y);
double x, y;
}
// #enddocregion semicolon-for-empty-body

Expand Down
4 changes: 2 additions & 2 deletions examples/misc/lib/language_tour/classes/employee.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ Map fetchDefaultData() => {}; // stub

// #docregion super
class Person {
String? firstName;

Person.fromJson(Map data) {
print('in Person');
}

String? firstName;
}

// #docregion method-then-constructor
Expand Down
4 changes: 2 additions & 2 deletions examples/misc/lib/language_tour/classes/immutable_point.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class ImmutablePoint {
const ImmutablePoint(this.x, this.y);

static const ImmutablePoint origin = ImmutablePoint(0, 0);

final double x, y;

const ImmutablePoint(this.x, this.y);
}
14 changes: 7 additions & 7 deletions examples/misc/lib/language_tour/classes/logger.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
// #docregion
class Logger {
final String name;
bool mute = false;

// _cache is library-private, thanks to
// the _ in front of its name.
static final Map<String, Logger> _cache = <String, Logger>{};

factory Logger(String name) {
return _cache.putIfAbsent(name, () => Logger._internal(name));
}
Expand All @@ -17,6 +10,13 @@ class Logger {

Logger._internal(this.name);

final String name;
bool mute = false;

// _cache is library-private, thanks to
// the _ in front of its name.
static final Map<String, Logger> _cache = <String, Logger>{};

void log(String msg) {
if (!mute) print(msg);
}
Expand Down
6 changes: 3 additions & 3 deletions examples/misc/lib/language_tour/classes/point.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ const double yOrigin = 0;

// #docregion class-with-distanceTo, constructor-initializer
class Point {
final double x;
final double y;

// #docregion class-with-distanceTo, named-constructor
Point(this.x, this.y);
// #enddocregion class-with-distanceTo, named-constructor
Expand Down Expand Up @@ -39,4 +36,7 @@ class Point {
return sqrt(dx * dx + dy * dy);
}
// #docregion constructor-initializer, named-constructor

final double x;
final double y;
}
6 changes: 3 additions & 3 deletions examples/misc/lib/language_tour/classes/point_alt.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
///
// #docregion idiomatic-constructor
class Point {
double x = 0;
double y = 0;

// Generative constructor with initializing formal parameters:
Point(this.x, this.y);
// #enddocregion idiomatic-constructor
Expand All @@ -32,4 +29,7 @@ class Point {
// #enddocregion initializer-list-with-assert

// #docregion idiomatic-constructor

double x = 0;
double y = 0;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class Point {
double x, y;

// The main constructor for this class.
Point(this.x, this.y);

// Delegates to the main constructor.
Point.alongXAxis(double x) : this(x, 0);

double x, y;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import 'dart:math';

class Point {
final double x;
final double y;
final double distanceFromOrigin;

Point(double x, double y)
: x = x,
y = y,
distanceFromOrigin = sqrt(x * x + y * y);

final double x;
final double y;
final double distanceFromOrigin;
}

void main() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
// #docregion positional, named
class Vector2d {
// #enddocregion positional
// ...

// #enddocregion named
// #docregion positional
final double x;
final double y;

Vector2d(this.x, this.y);
// #enddocregion positional

// #docregion named
Vector2d.named({required this.x, required this.y});
// #docregion positional
}
// #enddocregion named

class Vector3d extends Vector2d {
// #docregion positional, named
// #enddocregion positional
// ...

// #enddocregion named
// #docregion positional
final double z;
final double x;
final double y;
// #docregion positional
}

class Vector3d extends Vector2d {
// #docregion positional
// Forward the x and y parameters to the default super constructor like:
// Vector3d(final double x, final double y, this.z) : super(x, y);
Vector3d(super.x, super.y, this.z);
Expand All @@ -34,6 +31,14 @@ class Vector3d extends Vector2d {
// Vector3d.yzPlane({required double y, required this.z})
// : super.named(x: 0, y: y);
Vector3d.yzPlane({required super.y, required this.z}) : super.named(x: 0);
// #enddocregion named

// #enddocregion positional
// ...

// #enddocregion named
// #docregion positional
final double z;
// #docregion positional
}
// #enddocregion positional, named
Loading
Loading