Skip to content

Commit ae95904

Browse files
authored
[wildcard-variables] Allow super._. (#3845)
* [wildcard-variables] Allow super._. * Fix comments.
1 parent c8f7029 commit ae95904

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

working/wildcards/feature-specification.md

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Author: Bob Nystrom
44

55
Status: In-progress
66

7-
Version 1.4
7+
Version 1.5
88

99
## Motivation
1010

@@ -317,37 +317,45 @@ class C {
317317

318318
### Super parameters
319319

320-
An occurrence of `super._` as a declaration of a formal parameter in a
321-
constructor is a compile-time error. This error also occurs in the case
322-
where the super parameter has an explicitly declared type and/or default
323-
value.
324-
325-
*`super._` is not an error everywhere: In a method body it could be an
326-
invocation of an inherited getter named `_`.*
320+
The positional formal parameter `super._` is still allowed in non-redirecting
321+
generative constructors. Such a parameter forwards the argument's value to the
322+
super constructor invocation.
327323

328324
```dart
329-
class B {
330-
final _;
331-
B(this._);
325+
class A {
326+
final int x, y;
327+
A(this.x, this.y);
332328
}
333329
334-
class C extends B {
335-
C(super._); // Error.
330+
class B extends A {
331+
final int _;
332+
B(this._, super._, super._) { // No collision on multiple `super._`.
333+
print(_ + x + y); // Refers to `B._`, `A.x` and `A.y`.
334+
}
336335
}
337336
```
338337

339-
*The desugared meaning of a super parameter includes a reference to the
340-
parameter in the initializer list of the enclosing constructor declaration,
341-
but such references are not possible when the parameter name is a
342-
wildcard.*
338+
Similarly to `this._`, and unlike for example `super.x`, a `super._` does not
339+
introduce any identifier into the scope of the initializer list.
340+
341+
Because of that, multiple `super._` and `this._` parameters can occur in the
342+
same constructor without any name collision.
343+
344+
```dart
345+
class A {
346+
final int _, v;
347+
A(this._, this.v);
348+
}
343349
344-
*It may seem inconvenient that `super._` "does not work" in the last
345-
example, but we do not wish to create exceptions about the ability to refer
346-
to a declaration whose name is a wildcard, just so we can support this
347-
particular usage. The advice would be to use a different name than `_` for
348-
the instance variable in `B` in the first place, or using a different name
349-
in `B` and possibly `// ignore` a lint that may warn about the names being
350-
different.*
350+
class B extends A {
351+
B(super.x, super._)
352+
: assert(x > 0), // OK, `x` in scope.
353+
assert(_ >= 0) // Error: no `_` in scope.
354+
{
355+
print(_); // OK, means `this._` and refers to `A._`.
356+
}
357+
}
358+
```
351359

352360
### Extension types
353361

@@ -461,6 +469,9 @@ This lint is included in the core lint set which means that the scale of the bre
461469

462470
## Changelog
463471

472+
### 1.5
473+
- Allow `super._`. Discussion: [language/#3792](https://github.com/dart-lang/language/issues/3792)
474+
464475
### 1.4
465476
- Add section on import prefixes. Discussion: [language/#3799](https://github.com/dart-lang/language/issues/3799)
466477

0 commit comments

Comments
 (0)