Skip to content

Commit 6a3180f

Browse files
authored
Minor cleanup to constructors page (#6531)
Fixes #6519
1 parent c52fd4d commit 6a3180f

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

src/content/language/constructors.md

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,26 @@ Dart implements many types of constructors.
1616
Except for default constructors,
1717
these functions use the same name as their class.
1818

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.
19+
[Generative constructors][generative]
20+
: Creates new instances and initializes instance variables.
21+
22+
[Default constructors][default]
23+
: Used to create a new instance when a constructor hasn't been specified.
24+
It doesn't take arguments and isn't named.
25+
26+
[Named constructors][named]
27+
: Clarifies the purpose of a constructor or
28+
allows the creation of multiple constructors for the same class.
29+
30+
[Constant constructors][constant]
31+
: Creates instances as compile-time constants.
32+
33+
[Factory constructors][factory]
34+
: Either creates a new instance of a subtype or
35+
returns an existing instance from cache.
36+
37+
[Redirecting constructor][redirecting]
38+
: Forwards calls to another constructor in the same class.
3339

3440
[default]: #default-constructors
3541
[generative]: #generative-constructors
@@ -95,7 +101,7 @@ implement that constructor in the subclass.
95101
### Constant constructors
96102

97103
If your class produces unchanging objects, make these
98-
objects compile-time constants.
104+
objects compile-time constants.
99105
To make objects compile-time constants, define a `const` constructor
100106
with all instance variables set as `final`.
101107

@@ -118,7 +124,7 @@ To learn more, consult the section on [using constructors][].
118124

119125
A constructor might redirect to another constructor in the same class.
120126
A redirecting constructor has an empty body.
121-
The constructor uses `this` instead of the class name after a colon (:).
127+
The constructor uses `this` instead of the class name after a colon (`:`).
122128

123129
<?code-excerpt "point_redirecting.dart"?>
124130
```dart
@@ -139,15 +145,15 @@ When encountering one of following two cases of implementing a constructor,
139145
use the `factory` keyword:
140146

141147
* The constructor doesn't always create a new instance of its class.
142-
Although a factory constructor cannot return `null`,
148+
Although a factory constructor can't return `null`,
143149
it might return:
144-
150+
145151
* an existing instance from a cache instead of creating a new one
146152
* a new instance of a subtype
147153

148154
* You need to perform non-trivial work prior to constructing an instance.
149155
This could include checking arguments or doing any other processing
150-
that cannot be handled in the initializer list.
156+
that can't be handled in the initializer list.
151157

152158
:::tip
153159
You can also handle late initialization of a final variable
@@ -226,21 +232,22 @@ Dart allows you to supply a constructor as a parameter without calling it.
226232
Called a _tear-off_ (as you _tear off_ the parentheses)
227233
serves as a closure that invokes the constructor with the same parameters.
228234

229-
If the tear-off is a constructor with the same signature and return type
230-
as the method accepts, you can use the tear-off as a parameter or variable.
235+
If the tear-off is a constructor with the same
236+
signature and return type as the method accepts,
237+
you can use the tear-off as a parameter or variable.
231238

232239
Tear-offs differ from lambdas or anonymous functions.
233-
Lambdas serve as a wrapper for the constructor whereas a tear-off
234-
is the constructor.
240+
Lambdas serve as a wrapper for the constructor,
241+
whereas a tear-off is the constructor.
235242

236243
**Use Tear-Offs**
237244

238245
```dart tag=good
239-
// Use a tear-off for a named constructor:
240-
var strings = charCodes.map(String.fromCharCode);
246+
// Use a tear-off for a named constructor:
247+
var strings = charCodes.map(String.fromCharCode);
241248
242-
// Use a tear-off for an unnamed constructor:
243-
var buffers = charCodes.map(StringBuffer.new);
249+
// Use a tear-off for an unnamed constructor:
250+
var buffers = charCodes.map(StringBuffer.new);
244251
```
245252

246253
**Not Lambdas**
@@ -257,7 +264,7 @@ For more discussion, watch this Decoding Flutter video on tear-offs.
257264

258265
{% ytEmbed "OmCaloD7sis", "Dart Tear-offs | Decoding Flutter" %}
259266

260-
## Instance Variable Initialization
267+
## Instance variable initialization
261268

262269
Dart can initialize variables in three ways.
263270

0 commit comments

Comments
 (0)