Skip to content

Commit 7e985df

Browse files
authored
Delineate before and after examples more clearly (#3310)
1 parent 92ad4bd commit 7e985df

File tree

1 file changed

+33
-35
lines changed

1 file changed

+33
-35
lines changed

working/2364 - primary constructors/feature-specification.md

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ with two fields and a constructor:
4141

4242
```dart
4343
// Current syntax.
44-
4544
class Point {
4645
int x;
4746
int y;
@@ -54,7 +53,6 @@ concisely:
5453

5554
```dart
5655
// A declaration with the same meaning, using a primary constructor.
57-
5856
class Point(int x, int y);
5957
```
6058

@@ -66,12 +64,14 @@ library, so we should read the examples as "you can write this _or_ you can
6664
write that". So the example above would be shown as follows:
6765

6866
```dart
67+
// Current syntax.
6968
class Point {
7069
int x;
7170
int y;
7271
Point(this.x, this.y);
7372
}
7473
74+
// Using a primary constructor.
7575
class Point(int x, int y);
7676
```
7777

@@ -114,12 +114,14 @@ initializing formal anyway, so they will just need to be declared using a
114114
normal `external` variable declaration.
115115

116116
```dart
117+
// Current syntax.
117118
class ModifierClass {
118119
late int x;
119120
external double d;
120121
ModifierClass(this.x);
121122
}
122123
124+
// Using a primary constructor.
123125
class ModifierClass(this.x) {
124126
late int x;
125127
external double d;
@@ -132,6 +134,7 @@ class ModifierClass(this.x) {
132134
Super parameters can be declared in the same way as in a body constructor:
133135

134136
```dart
137+
// Current syntax.
135138
class A {
136139
final int a;
137140
A(this.a);
@@ -141,19 +144,22 @@ class B extends A {
141144
B(super.a);
142145
}
143146
147+
// Using a primary constructor.
144148
class A(final int a);
145149
class B(super.a) extends A;
146150
```
147151

148152
Next, the constructor can be named, and it can be constant:
149153

150154
```dart
155+
// Current syntax.
151156
class Point {
152157
final int x;
153158
final int y;
154159
const Point._(this.x, this.y);
155160
}
156161
162+
// Using a primary constructor.
157163
class const Point._(final int x, final int y);
158164
```
159165

@@ -182,16 +188,14 @@ declaration is an `extension type` or an `enum` declaration, the modifier
182188
from the formal parameter in the primary constructor:
183189

184190
```dart
185-
extension type I.name(int x); // Must use a primary constructor.
191+
// Current syntax.
186192
187193
class Point {
188194
final int x;
189195
final int y;
190196
const Point(this.x, this.y);
191197
}
192198
193-
class const Point(int x, int y);
194-
195199
enum E {
196200
one('a'),
197201
two('b');
@@ -200,7 +204,14 @@ enum E {
200204
const E(this.s);
201205
}
202206
207+
// Using a primary constructor.
208+
209+
class const Point(int x, int y);
210+
203211
enum E(String s) { one('a'), two('b') }
212+
213+
extension type I.name(int x); // Must use a primary constructor.
214+
204215
```
205216

206217
This mechanism follows an existing pattern, where `const` modifiers can be
@@ -218,78 +229,63 @@ Optional parameters can be declared as usual in a primary constructor, with
218229
default values that must be constant as usual:
219230

220231
```dart
232+
// Current syntax.
221233
class Point {
222234
int x;
223235
int y;
224236
Point(this.x, [this.y = 0]);
225237
}
226238
239+
// Using a primary constructor.
227240
class Point(int x, [int y = 0]);
228241
```
229242

230243
Similarly for named parameters, required or not:
231244

232245
```dart
246+
// Current syntax.
233247
class Point {
234248
int x;
235249
int y;
236250
Point(this.x, {required this.y});
237251
}
238252
253+
// Using a primary constructor.
239254
class Point(int x, {required int y});
240255
```
241256

242257
In this declaration it is possible to omit the modifier `required` on the
243258
named parameter `y`, because it is implied by the fact that the type of `y`
244259
is non-nullable (potentially non-nullable is enough).
245260

246-
The current scope for the default values in the primary constructor is the
247-
enclosing library scope. This means that a naive copy/paste operation on
248-
the source code could change the meaning of the default value. In that case
249-
a new way to denote the given value is established. For example, consider
250-
this class using a primary constructor:
251-
252-
```dart
253-
static const d = 42;
254-
255-
class Point(int x, [int y = d]) {
256-
void d() {}
257-
}
258-
```
259-
260-
This corresponds to the following class without a primary constructor:
261-
262-
```dart
263-
static const d = 42;
264-
static const _freshName = d; // Eliminate the name clash.
265-
266-
class Point {
267-
int x;
268-
int y;
269-
Point(this.x, [this.y = _freshName]);
270-
void d() {}
271-
}
272-
```
273-
274261
The class header can have additional elements, just like class headers
275262
where there is no primary constructor:
276263

277264
```dart
265+
// Current syntax.
278266
class D<TypeVariable extends Bound> extends A with M implements B, C {
279267
final int x;
280268
final int y;
281269
const D.named(this.x, [this.y = 0]);
282270
}
283271
272+
// Using a primary constructor.
284273
class const D<TypeVariable extends Bound>.named(int x, [int y = 0])
285274
extends A with M implements B, C;
286275
```
287276

288277
In the case where the header gets unwieldy it is possible to declare the
289-
primary constructor in the body of the class, which is also equivalent to
290-
the previous examples:
278+
primary constructor in the body of the class using the `primary` keyword:
291279

292280
```dart
281+
// Current syntax.
282+
class D<TypeVariable extends Bound> extends A with M implements B, C {
283+
final int x;
284+
final int y;
285+
const D.named(this.x, [this.y = 0]);
286+
}
287+
288+
// Using a primary constructor.
293289
class D<TypeVariable extends Bound> extends A with M implements B, C {
294290
primary const D.named(int x, [int y = 0]);
295291
}
@@ -303,6 +299,7 @@ same way as a primary constructor in the header of the declaration. For
303299
example:
304300

305301
```dart
302+
// Current syntax.
306303
class A {
307304
A(String _);
308305
}
@@ -337,6 +334,7 @@ class E extends A {
337334
}
338335
}
339336
337+
// Using a primary constructor.
340338
class E extends A {
341339
external int y;
342340
int z;

0 commit comments

Comments
 (0)