Skip to content

Commit 638cb9e

Browse files
author
Tony Sansone
committed
Update per @natebosch review
1 parent 4350be7 commit 638cb9e

File tree

2 files changed

+73
-58
lines changed

2 files changed

+73
-58
lines changed

examples/misc/lib/language_tour/classes/point_alt.dart

+8-8
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
///
77
// #docregion idiomatic-constructor
88
class Point {
9-
double x = 0;
10-
double y = 0;
9+
// Initializer list of variables and values
10+
double x = 2.0;
11+
double y = 2.0;
1112

1213
// Generative constructor with initializing formal parameters:
1314
Point(this.x, this.y);
@@ -40,8 +41,8 @@ class PointA {
4041
double x = 1.0;
4142
double y = 2.0;
4243

43-
// The parameterless constructor is not even be needed to set to (1.0,2.0)
44-
PointA();
44+
// The implicit default constructor sets these variables to (1.0,2.0)
45+
// PointA();
4546

4647
@override
4748
String toString() {
@@ -61,7 +62,6 @@ class PointB {
6162

6263
// Initializing formal parameters can also be optional.
6364
PointB.optional([this.x = 0.0, this.y = 0.0]);
64-
PointB.named({required this.x, required this.y});
6565
}
6666
// #enddocregion initialize-formal
6767

@@ -72,16 +72,16 @@ class PointC {
7272

7373
// Generative constructor with initializing formal parameters
7474
// with default values
75-
PointC({this.x = 1.0, this.y = 1.0});
75+
PointC.named({this.x = 1.0, this.y = 1.0});
7676

7777
@override
7878
String toString() {
79-
return 'PointC($x,$y)';
79+
return 'PointC.named($x,$y)';
8080
}
8181
}
8282

8383
// Constructor using named variables.
84-
final pointC = PointC(x: 2.0, y: 2.0);
84+
final pointC = PointC.named(x: 2.0, y: 2.0);
8585
// #enddocregion initialize-named
8686

8787
// #docregion initialize-null

src/content/language/constructors.md

+65-50
Original file line numberDiff line numberDiff line change
@@ -10,62 +10,61 @@ nextpage:
1010
title: Methods
1111
---
1212

13-
Constructors create instances of classes.
14-
15-
* To declare a constructor, create a function named the same as its class.
16-
You can add an optional additional identifier as described in
17-
[Named constructors](#named-constructors).
18-
19-
* To instantiate a class, use the
20-
[generative constructor](#generative-constructors).
21-
22-
* To instantiate any instance variables,
23-
[initialize formal parameters](#parameter-initialization).
13+
Constructors are special functions that create instances of classes.
14+
15+
Dart implements many types of constructors.
16+
Except for default constructors,
17+
these functions use the same name as their class.
18+
19+
* [Generative constructors][generative]: Creates new instances and
20+
initializes instance variables.
21+
* [Default constructors][default]: Used to create a new instance when a
22+
constructor hasn't been specified. It doesn't take arguments and
23+
isn't named.
24+
* [Named constructors][named]: Clarifies the purpose of
25+
a constructor or allows the creation of multiple constructors for
26+
the same class.
27+
* [Constant constructors][constant]: Creates instances as compile-type
28+
constants.
29+
* [Factory constructors][factory]: Either creates a new instance of a
30+
subtype or returns an existing instance from cache.
31+
* [Redirecting constructor][redirecting]: Forwards calls to another
32+
constructor in the same class.
33+
34+
[default]: #default-constructors
35+
[generative]: #generative-constructors
36+
[named]: #named-constructors
37+
[constant]: #constant-constructors
38+
[factory]: #factory-constructors
39+
[redirecting]: #redirecting-constructors
2440

2541
<?code-excerpt path-base="misc/lib/language_tour/classes"?>
2642

2743
<?code-excerpt replace="/ *\/\/\s+ignore_for_file:[^\n]+\n//g; /(^|\n) *\/\/\s+ignore:[^\n]+\n/$1/g; /(\n[^\n]+) *\/\/\s+ignore:[^\n]+\n/$1\n/g; / *\/\/\s+ignore:[^\n]+//g; /([A-Z]\w*)\d\b/$1/g"?>
2844

2945
## Types of constructors
3046

31-
### Constructor inheritance
32-
33-
_Subclasses_, or child classes, don't inherit *constructors*
34-
from their _superclass_, or immediate parent class.
35-
If a class doesn't declare a constructor, it can only use the
36-
[default constructor](#default-constructors).
37-
38-
A class can inherit the _parameters_ of a superclass.
39-
These are called [super parameters](#super-parameters)
40-
A subclass without constructor declarations can only use
41-
a [default constructor](#default-constructors).
42-
43-
Constructors work in a somewhat similar way to
44-
how you call a chain of static methods.
45-
Each subclass can call its superclass's constructor to initialize an instance,
46-
like a subclass can call a superclass's static method.
47-
This process doesn't involve any "inheritance".
48-
49-
### Default constructors
50-
51-
If you don't declare a constructor, Dart uses the default constructor.
52-
The default constructor has no arguments and no name.
53-
5447
### Generative constructors
5548

56-
To instantiate a class, use the generative constructor.
49+
To instantiate a class, use a generative constructor.
5750

5851
<?code-excerpt "point_alt.dart (idiomatic-constructor)" plaster="none"?>
5952
```dart
6053
class Point {
61-
double x = 0;
62-
double y = 0;
54+
// Initializer list of variables and values
55+
double x = 2.0;
56+
double y = 2.0;
6357
6458
// Generative constructor with initializing formal parameters:
6559
Point(this.x, this.y);
6660
}
6761
```
6862

63+
### Default constructors
64+
65+
If you don't declare a constructor, Dart uses the default constructor.
66+
The default constructor is a generative constructor without arguments or name.
67+
6968
### Named constructors
7069

7170
Use a named constructor to implement multiple constructors for a class
@@ -201,14 +200,14 @@ To further illustrate, consider the following example.
201200
```dart
202201
// If you invoke the super constructor (`super(0)`) with any
203202
// positional arguments, using a super parameter (`super.x`)
204-
// returns an error.
203+
// results in an error.
205204
Vector3d.xAxisError(super.x): z = 0, super(0); // BAD
206205
```
207206

208207
This named constructor tries to set the `x` value twice:
209208
once in the super constructor and once as a
210209
positional super parameter.
211-
As both address the `x` positional parameter, this returns an error.
210+
As both address the `x` positional parameter, this results in an error.
212211

213212
When the super constructor has named arguments, you can split them
214213
between named super parameters (`super.y` in the next example)
@@ -251,6 +250,7 @@ class ImmutablePoint {
251250
```
252251

253252
Constant constructors don't always create constants.
253+
They might be invoked in a non-`const` context.
254254
To learn more, consult the section on [using constructors][].
255255

256256
### Redirecting constructors
@@ -274,7 +274,7 @@ class Point {
274274

275275
### Factory constructors
276276

277-
When encountering one of two cases of implementing a constructor,
277+
When encountering one of following two cases of implementing a constructor,
278278
use the `factory` keyword:
279279

280280
* The constructor doesn't always create a new instance of its class.
@@ -359,22 +359,38 @@ Redirecting factories have several advantages:
359359
* A redirecting factory constructor avoids the need for forwarders
360360
to repeat the formal parameters and their default values.
361361

362-
## Parameter initialization
362+
## Constructor inheritance
363+
364+
_Subclasses_, or child classes, don't inherit *constructors*
365+
from their _superclass_, or immediate parent class.
366+
If a class doesn't declare a constructor, it can only use the
367+
[default constructor](#default-constructors).
368+
369+
A class can inherit the _parameters_ of a superclass.
370+
These are called [super parameters](#super-parameters)
371+
372+
Constructors work in a somewhat similar way to
373+
how you call a chain of static methods.
374+
Each subclass can call its superclass's constructor to initialize an instance,
375+
like a subclass can call a superclass's static method.
376+
This process doesn't "inherit" constructor bodies or signatures.
377+
378+
## Instance Variable Initialization
363379

364-
Dart can initialize parameters in a constructor in three ways.
380+
Dart can initialize variables in three ways.
365381

366-
### Initialize parameters when declaring variables
382+
### Initialize instance variables in the declaration
367383

368-
Initialize the constructor parameters when you declare variables.
384+
Initialize the instance variables when you declare the variables.
369385

370386
<?code-excerpt "point_alt.dart (initialize-declaration)" plaster="none"?>
371387
```dart
372388
class PointA {
373389
double x = 1.0;
374390
double y = 2.0;
375391
376-
// The parameterless constructor is not even be needed to set to (1.0,2.0)
377-
PointA();
392+
// The implicit default constructor sets these variables to (1.0,2.0)
393+
// PointA();
378394
379395
@override
380396
String toString() {
@@ -419,7 +435,6 @@ class PointB {
419435
420436
// Initializing formal parameters can also be optional.
421437
PointB.optional([this.x = 0.0, this.y = 0.0]);
422-
PointB.named({required this.x, required this.y});
423438
}
424439
```
425440

@@ -451,16 +466,16 @@ class PointC {
451466
452467
// Generative constructor with initializing formal parameters
453468
// with default values
454-
PointC({this.x = 1.0, this.y = 1.0});
469+
PointC.named({this.x = 1.0, this.y = 1.0});
455470
456471
@override
457472
String toString() {
458-
return 'PointC($x,$y)';
473+
return 'PointC.named($x,$y)';
459474
}
460475
}
461476
462477
// Constructor using named variables.
463-
final pointC = PointC(x: 2.0, y: 2.0);
478+
final pointC = PointC.named(x: 2.0, y: 2.0);
464479
```
465480

466481
All variables introduced from initializing formal parameters are both

0 commit comments

Comments
 (0)