Skip to content

Commit a13bf5c

Browse files
author
Tony Sansone
committed
Fixed code excerpts
1 parent 7f501ef commit a13bf5c

File tree

1 file changed

+52
-32
lines changed

1 file changed

+52
-32
lines changed

src/content/language/constructors.md

+52-32
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ Before the constructor body (if any),
9292
specify the superclass constructor after a colon (`:`).
9393

9494
In the following example,
95-
the constructor for the `Employee` class calls the named constructor
96-
for its superclass, `Person`. Click **Run** to execute the code.
95+
the `Employee` class constructor calls the named constructor
96+
for its superclass, `Person`. To execute the following code, click **Run**.
9797

9898
<?code-excerpt "employee.dart (super)" plaster="none"?>
9999
```dart:run-dartpad:height-450px:ga_id-non_default_superclass_constructor
@@ -187,8 +187,9 @@ class Vector2d {
187187
class Vector3d extends Vector2d {
188188
final double z;
189189
190-
// Conflicts with the super constructor Vector2d(this.x, this.y)
191-
Vector3d(super.y, this.z);
190+
// Forward the x and y parameters to the default super constructor like:
191+
// Vector3d(final double x, final double y, this.z) : super(x, y);
192+
Vector3d(super.x, super.y, this.z);
192193
}
193194
```
194195

@@ -215,12 +216,11 @@ and named arguments to the super constructor invocation
215216
```dart
216217
class Vector2d {
217218
// ...
218-
219219
Vector2d.named({required this.x, required this.y});
220220
}
221221
222222
class Vector3d extends Vector2d {
223-
// ...
223+
final double z;
224224
225225
// Forward the y parameter to the named super constructor like:
226226
// Vector3d.yzPlane({required double y, required this.z})
@@ -229,6 +229,27 @@ class Vector3d extends Vector2d {
229229
}
230230
```
231231

232+
### Constant constructors
233+
234+
If your class produces unchanging objects, make these
235+
objects compile-time constants.
236+
To make objects compile-time constants, define a `const` constructor
237+
with all instance variables set as `final`.
238+
239+
<?code-excerpt "immutable_point.dart"?>
240+
```dart
241+
class ImmutablePoint {
242+
static const ImmutablePoint origin = ImmutablePoint(0, 0);
243+
244+
final double x, y;
245+
246+
const ImmutablePoint(this.x, this.y);
247+
}
248+
```
249+
250+
Constant constructors don't always create constants.
251+
To learn more, consult the section on [using constructors][].
252+
232253
### Redirecting constructors
233254

234255
A constructor might redirect to another constructor in the same class.
@@ -248,26 +269,6 @@ class Point {
248269
}
249270
```
250271

251-
### Constant constructors
252-
253-
If your class produces objects that never change, you can make these
254-
objects compile-time constants. To do this, define a `const` constructor
255-
and make sure that all instance variables are `final`.
256-
257-
<?code-excerpt "immutable_point.dart"?>
258-
```dart
259-
class ImmutablePoint {
260-
static const ImmutablePoint origin = ImmutablePoint(0, 0);
261-
262-
final double x, y;
263-
264-
const ImmutablePoint(this.x, this.y);
265-
}
266-
```
267-
268-
Constant constructors don't always create constants.
269-
To learn more, consult the section on [using constructors][].
270-
271272
### Factory constructors
272273

273274
When encountering one of two cases of implementing a constructor,
@@ -336,6 +337,25 @@ var logMap = {'name': 'UI'};
336337
var loggerJson = Logger.fromJson(logMap);
337338
```
338339

340+
### Redirecting factory constructors
341+
342+
A redirecting factory constructor specifies a call to a constructor of another
343+
class to use whenever someone makes a call to the redirecting constructor.
344+
345+
```dart
346+
factory Listenable.merge(List<Listenable> listenables) = _MergingListenable
347+
```
348+
349+
It might appear that ordinary factory constructors
350+
could create and return instances of other classes.
351+
This would make redirecting factories unnecessary.
352+
Redirecting factories have several advantages:
353+
354+
* An abstract class might provide a constant constructor
355+
that uses the constant constructor of another class.
356+
* A redirecting factory constructor avoids the need for forwarders
357+
to repeat the formal parameters and their default values.
358+
339359
## Parameter initialization
340360

341361
Dart can initialize parameters in a constructor in three ways.
@@ -347,8 +367,8 @@ Initialize the constructor parameters when you declare variables.
347367
<?code-excerpt "point_alt.dart (initialize-declaration)" plaster="none"?>
348368
```dart
349369
class PointA {
350-
[!double x = 1.0;!]
351-
[!double y = 2.0;!]
370+
double x = 1.0;
371+
double y = 2.0;
352372
353373
// The parameterless constructor is not even be needed to set to (1.0,2.0)
354374
PointA();
@@ -415,7 +435,7 @@ class PointC {
415435
416436
// Generative constructor with initializing formal parameters
417437
// with default values
418-
[!PointC({this.x = 1.0, this.y = 1.0});!]
438+
PointC({this.x = 1.0, this.y = 1.0});
419439
420440
@override
421441
String toString() {
@@ -424,7 +444,7 @@ class PointC {
424444
}
425445
426446
// Constructor using named variables.
427-
[!final pointC = PointC(x: 2.0, y: 2.0);!]
447+
final pointC = PointC(x: 2.0, y: 2.0);
428448
```
429449

430450
All variables introduced from initializing formal parameters are both
@@ -440,8 +460,8 @@ The constructor parameters could be set as nullable and not be initialized.
440460
<?code-excerpt "point_alt.dart (initialize-null)" plaster="none"?>
441461
```dart
442462
class PointD {
443-
[!double? x;!] // null if not set in constructor
444-
[!double? y;!] // null if not set in constructor
463+
double? x; // null if not set in constructor
464+
double? y; // null if not set in constructor
445465
446466
// Generative constructor with initializing formal parameters
447467
PointD(this.x, this.y);

0 commit comments

Comments
 (0)