Skip to content

Commit 25a9cb3

Browse files
authored
[wildcard-variables] Add more explicit details on functions and records. (#3802)
* [wildcard-variables] Add more explicit details on functions and records. * Rewrite record type positional fields and add newline. * Named fields of record types are unchanged.
1 parent 8a4cc1a commit 25a9cb3

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

working/wildcards/feature-specification.md

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

55
Status: In-progress
66

7-
Version 1.2
7+
Version 1.3
8+
9+
## Motivation
810

911
Pattern matching brings a new way to declare variables. Inside patterns, any
1012
variable whose name is `_` is considered a "wildcard". It behaves like a
@@ -74,6 +76,8 @@ library privacy comes into play.
7476
7577
## Proposal
7678
79+
### Local declarations
80+
7781
A *local declaration* is any of:
7882
7983
* Function parameters. This includes top-level functions, local functions,
@@ -85,6 +89,10 @@ A *local declaration* is any of:
8589
Foo(_, this._, super._, void _(), {_}) {}
8690
8791
list.where((_) => true);
92+
93+
void f(void g(_, _)) {}
94+
95+
typedef T = void Function(String _, String _);
8896
```
8997
9098
* Local variable declaration statement variables.
@@ -127,8 +135,35 @@ means you can have multiple local declarations named `_` in the same namespace
127135
without a collision error. The initializer, if there is one, is still executed,
128136
but the value is not accessible.
129137
130-
Other declarations: top-level variables, top-level function names, type names,
131-
member names, etc. are unchanged. They can be named `_` as they are today.
138+
### Record type positional fields
139+
140+
```dart
141+
typedef R = (String _, String _);
142+
(int _, int _) record;
143+
```
144+
145+
It is currently an error for a record field name to begin with `_`
146+
(including just a bare `_`). We relax that error to only apply to record
147+
fields whose name begins with `_` followed by at least one other character
148+
(even if those later character(s) are `_`).
149+
150+
Named fields of record types are unchanged. It is still a compile-time error for a named field name to start with `_`.
151+
152+
### Local function declarations
153+
154+
```dart
155+
void f() {
156+
_() {} // Error.
157+
_(); // Error.
158+
}
159+
```
160+
161+
It's an error to declare a local function declaration named `_`.
162+
163+
### Other declarations
164+
165+
Top-level variables, top-level function names, type names, member names,
166+
etc. are unchanged. They can be named `_` as they are today.
132167

133168
We do not change how identifier *expressions* behave. Members can be named `_`
134169
and you can access them from inside the class where the member is declared
@@ -403,6 +438,11 @@ This lint is included in the core lint set which means that the scale of the bre
403438

404439
## Changelog
405440

441+
### 1.3
442+
443+
- Add section on local function declarations. Discussion: [language/#3790](https://github.com/dart-lang/language/issues/3790)
444+
- Add section on record type positionals and more examples with function types. Discussion: [language/#3791](https://github.com/dart-lang/language/issues/3791)
445+
406446
### 1.2
407447

408448
- Add information about the [`no_wildcard_variable_uses`](https://dart.dev/tools/linter-rules/no_wildcard_variable_uses) lint.

0 commit comments

Comments
 (0)