@@ -4,7 +4,7 @@ Author: Bob Nystrom
4
4
5
5
Status: In-progress
6
6
7
- Version 1.0
7
+ Version 1.1
8
8
9
9
Pattern matching brings a new way to declare variables. Inside patterns, any
10
10
variable whose name is ` _ ` is considered a "wildcard". It behaves like a
@@ -99,8 +99,8 @@ A *local declaration* is any of:
99
99
* For loop variable declarations.
100
100
101
101
```dart
102
- for (_ = 0;;) {}
103
- for (_ in list) {}
102
+ for (int _ = 0;;) {}
103
+ for (var _ in list) {}
104
104
```
105
105
106
106
* Catch clause parameters.
@@ -117,11 +117,12 @@ A *local declaration* is any of:
117
117
118
118
```dart
119
119
class T<_> {}
120
+ void genericFunction<_>() {}
120
121
121
122
takeGenericCallback(<_>() => true);
122
123
```
123
124
124
- A local declaration whose name is `_` does not bind anything to that name. This
125
+ A local declaration whose name is `_` does not bind that name to anything . This
125
126
means you can have multiple local declarations named `_` in the same namespace
126
127
without a collision error. The initializer, if there is one, is still executed,
127
128
but the value is not accessible.
@@ -138,12 +139,12 @@ class C {
138
139
var _ = 'bound';
139
140
140
141
test() {
141
- print(_); // Prints "bounnd ".
142
+ print(_); // Prints "bound ".
142
143
}
143
144
}
144
145
```
145
146
146
- Likewise with a top-level named ` _ ` :
147
+ Likewise with a top-level declaration named ` _ ` :
147
148
148
149
``` dart
149
150
var _ = 'ok';
@@ -207,8 +208,8 @@ quite confusing. In practice, we expect reasonable users will not name fields
207
208
208
209
### Initializing formals
209
210
210
- An initializing formal named ` _ ` does still initialize a field named ` _ ` (and
211
- you can still have a field with that name):
211
+ A positional initializing formal named ` _ ` does still initialize a field
212
+ named ` _ ` (and you can still have a field with that name):
212
213
213
214
``` dart
214
215
class C {
@@ -218,17 +219,29 @@ class C {
218
219
}
219
220
```
220
221
222
+ * Note that it is already a compile-time error if a named initializing
223
+ formal has the name ` _ ` . This is a special case of the rule that it is an
224
+ error for a named formal parameter to have a name that starts with ` _ ` .*
225
+
226
+ ``` dart
227
+ class C {
228
+ var _;
229
+
230
+ C({this._}); // Error.
231
+ }
232
+ ```
233
+
221
234
But no * parameter* with that name is bound, which means ` _ ` can't be accessed
222
- inside the initializer list. In the body is fine, since that refers to the
223
- field, not the parameter:
235
+ inside the initializer list. The name ` _ ` can be used in the body, but this
236
+ is a reference to the field, not the parameter:
224
237
225
238
``` dart
226
239
class C {
227
240
var _;
228
241
var other;
229
242
230
243
C(this._)
231
- : other = _ { // <-- Error. No "_" in scope .
244
+ : other = _ { // <-- Error, cannot access `this` .
232
245
print(_); // OK. Prints the field.
233
246
}
234
247
}
@@ -244,6 +257,68 @@ class C {
244
257
}
245
258
```
246
259
260
+ ### Super parameters
261
+
262
+ An occurrence of ` super._ ` as a declaration of a formal parameter in a
263
+ constructor is a compile-time error. This error also occurs in the case
264
+ where the super parameter has an explicitly declared type and/or default
265
+ value.
266
+
267
+ * ` super._ ` is not an error everywhere: In a method body it could be an
268
+ invocation of an inherited getter named ` _ ` .*
269
+
270
+ ``` dart
271
+ class B {
272
+ final _;
273
+ B(this._);
274
+ }
275
+
276
+ class C {
277
+ C(super._); // Error.
278
+ }
279
+ ```
280
+
281
+ * The desugared meaning of a super parameter includes a reference to the
282
+ parameter in the initializer list of the enclosing constructor declaration,
283
+ but such references are not possible when the parameter name is a
284
+ wildcard.*
285
+
286
+ * It may seem inconvenient that ` super._ ` "does not work" in the last
287
+ example, but we do not wish to create exceptions about the ability to refer
288
+ to a declaration whose name is a wildcard, just so we can support this
289
+ particular usage. The advice would be to use a different name than ` _ ` for
290
+ the instance variable in ` B ` in the first place, or using a different name
291
+ in ` B ` and possibly ` // ignore ` a lint that may warn about the names being
292
+ different.*
293
+
294
+ ### Extension types
295
+
296
+ An extension type declaration has a ` <representationDeclaration> `
297
+ which is similar to a formal parameter list of a function declaration.
298
+
299
+ * It always declares exactly one mandatory positional parameter, and the
300
+ meaning of this declaration is that it introduces a formal parameter of a
301
+ constructor of the enclosing extension type as well as a final instance
302
+ variable declaration, also known as the representation variable of the
303
+ extension type.*
304
+
305
+ This parameter can have the declared name ` _ ` . This means that the
306
+ representation variable is named ` _ ` , and no formal parameter name is
307
+ introduced into any scopes.
308
+
309
+ ``` dart
310
+ extension type E(int _) {
311
+ int get value => _; // OK, the representation variable name is `_`.
312
+ int get sameValue => this._; // OK.
313
+ }
314
+ ```
315
+
316
+ * Currently, the formal parameter introduced by a
317
+ ` <representationDeclaration> ` is not in scope for any code, anyway.
318
+ However, future generalizations such as primary constructors could
319
+ introduce it into a scope. At that time it does matter that there is no
320
+ formal parameter name.*
321
+
247
322
### Unused variable warnings
248
323
249
324
Dart tools currently warn if you have an unused local declaration. If the
@@ -319,3 +394,13 @@ public API.
319
394
However, this * is* a breaking change. If this ships in the same version as
320
395
pattern matching, we can gate it behind a language version and only break code
321
396
when it upgrades to that version.
397
+
398
+ ## Changelog
399
+
400
+ ### 1.1
401
+
402
+ - Add rules about ` super._ ` and about extension types.
403
+
404
+ ### 1.0
405
+
406
+ - Initial version
0 commit comments