@@ -41,7 +41,6 @@ with two fields and a constructor:
41
41
42
42
``` dart
43
43
// Current syntax.
44
-
45
44
class Point {
46
45
int x;
47
46
int y;
@@ -54,7 +53,6 @@ concisely:
54
53
55
54
``` dart
56
55
// A declaration with the same meaning, using a primary constructor.
57
-
58
56
class Point(int x, int y);
59
57
```
60
58
@@ -66,12 +64,14 @@ library, so we should read the examples as "you can write this _or_ you can
66
64
write that". So the example above would be shown as follows:
67
65
68
66
``` dart
67
+ // Current syntax.
69
68
class Point {
70
69
int x;
71
70
int y;
72
71
Point(this.x, this.y);
73
72
}
74
73
74
+ // Using a primary constructor.
75
75
class Point(int x, int y);
76
76
```
77
77
@@ -114,12 +114,14 @@ initializing formal anyway, so they will just need to be declared using a
114
114
normal ` external ` variable declaration.
115
115
116
116
``` dart
117
+ // Current syntax.
117
118
class ModifierClass {
118
119
late int x;
119
120
external double d;
120
121
ModifierClass(this.x);
121
122
}
122
123
124
+ // Using a primary constructor.
123
125
class ModifierClass(this.x) {
124
126
late int x;
125
127
external double d;
@@ -132,6 +134,7 @@ class ModifierClass(this.x) {
132
134
Super parameters can be declared in the same way as in a body constructor:
133
135
134
136
``` dart
137
+ // Current syntax.
135
138
class A {
136
139
final int a;
137
140
A(this.a);
@@ -141,19 +144,22 @@ class B extends A {
141
144
B(super.a);
142
145
}
143
146
147
+ // Using a primary constructor.
144
148
class A(final int a);
145
149
class B(super.a) extends A;
146
150
```
147
151
148
152
Next, the constructor can be named, and it can be constant:
149
153
150
154
``` dart
155
+ // Current syntax.
151
156
class Point {
152
157
final int x;
153
158
final int y;
154
159
const Point._(this.x, this.y);
155
160
}
156
161
162
+ // Using a primary constructor.
157
163
class const Point._(final int x, final int y);
158
164
```
159
165
@@ -182,16 +188,14 @@ declaration is an `extension type` or an `enum` declaration, the modifier
182
188
from the formal parameter in the primary constructor:
183
189
184
190
``` dart
185
- extension type I.name(int x); // Must use a primary constructor .
191
+ // Current syntax .
186
192
187
193
class Point {
188
194
final int x;
189
195
final int y;
190
196
const Point(this.x, this.y);
191
197
}
192
198
193
- class const Point(int x, int y);
194
-
195
199
enum E {
196
200
one('a'),
197
201
two('b');
@@ -200,7 +204,14 @@ enum E {
200
204
const E(this.s);
201
205
}
202
206
207
+ // Using a primary constructor.
208
+
209
+ class const Point(int x, int y);
210
+
203
211
enum E(String s) { one('a'), two('b') }
212
+
213
+ extension type I.name(int x); // Must use a primary constructor.
214
+
204
215
```
205
216
206
217
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
218
229
default values that must be constant as usual:
219
230
220
231
``` dart
232
+ // Current syntax.
221
233
class Point {
222
234
int x;
223
235
int y;
224
236
Point(this.x, [this.y = 0]);
225
237
}
226
238
239
+ // Using a primary constructor.
227
240
class Point(int x, [int y = 0]);
228
241
```
229
242
230
243
Similarly for named parameters, required or not:
231
244
232
245
``` dart
246
+ // Current syntax.
233
247
class Point {
234
248
int x;
235
249
int y;
236
250
Point(this.x, {required this.y});
237
251
}
238
252
253
+ // Using a primary constructor.
239
254
class Point(int x, {required int y});
240
255
```
241
256
242
257
In this declaration it is possible to omit the modifier ` required ` on the
243
258
named parameter ` y ` , because it is implied by the fact that the type of ` y `
244
259
is non-nullable (potentially non-nullable is enough).
245
260
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
-
274
261
The class header can have additional elements, just like class headers
275
262
where there is no primary constructor:
276
263
277
264
``` dart
265
+ // Current syntax.
278
266
class D<TypeVariable extends Bound> extends A with M implements B, C {
279
267
final int x;
280
268
final int y;
281
269
const D.named(this.x, [this.y = 0]);
282
270
}
283
271
272
+ // Using a primary constructor.
284
273
class const D<TypeVariable extends Bound>.named(int x, [int y = 0])
285
274
extends A with M implements B, C;
286
275
```
287
276
288
277
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:
291
279
292
280
``` 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.
293
289
class D<TypeVariable extends Bound> extends A with M implements B, C {
294
290
primary const D.named(int x, [int y = 0]);
295
291
}
@@ -303,6 +299,7 @@ same way as a primary constructor in the header of the declaration. For
303
299
example:
304
300
305
301
``` dart
302
+ // Current syntax.
306
303
class A {
307
304
A(String _);
308
305
}
@@ -337,6 +334,7 @@ class E extends A {
337
334
}
338
335
}
339
336
337
+ // Using a primary constructor.
340
338
class E extends A {
341
339
external int y;
342
340
int z;
0 commit comments