Skip to content

Commit

Permalink
[wildcard-variables] Add more explicit details on functions and recor…
Browse files Browse the repository at this point in the history
…ds. (#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.
  • Loading branch information
kallentu committed May 15, 2024
1 parent 8a4cc1a commit 25a9cb3
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions working/wildcards/feature-specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ Author: Bob Nystrom

Status: In-progress

Version 1.2
Version 1.3

## Motivation

Pattern matching brings a new way to declare variables. Inside patterns, any
variable whose name is `_` is considered a "wildcard". It behaves like a
Expand Down Expand Up @@ -74,6 +76,8 @@ library privacy comes into play.

## Proposal

### Local declarations

A *local declaration* is any of:

* Function parameters. This includes top-level functions, local functions,
Expand All @@ -85,6 +89,10 @@ A *local declaration* is any of:
Foo(_, this._, super._, void _(), {_}) {}
list.where((_) => true);
void f(void g(_, _)) {}
typedef T = void Function(String _, String _);
```

* Local variable declaration statement variables.
Expand Down Expand Up @@ -127,8 +135,35 @@ means you can have multiple local declarations named `_` in the same namespace
without a collision error. The initializer, if there is one, is still executed,
but the value is not accessible.

Other declarations: top-level variables, top-level function names, type names,
member names, etc. are unchanged. They can be named `_` as they are today.
### Record type positional fields

```dart
typedef R = (String _, String _);
(int _, int _) record;
```

It is currently an error for a record field name to begin with `_`
(including just a bare `_`). We relax that error to only apply to record
fields whose name begins with `_` followed by at least one other character
(even if those later character(s) are `_`).

Named fields of record types are unchanged. It is still a compile-time error for a named field name to start with `_`.

### Local function declarations

```dart
void f() {
_() {} // Error.
_(); // Error.
}
```

It's an error to declare a local function declaration named `_`.

### Other declarations

Top-level variables, top-level function names, type names, member names,
etc. are unchanged. They can be named `_` as they are today.

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

## Changelog

### 1.3

- Add section on local function declarations. Discussion: [language/#3790](https://github.com/dart-lang/language/issues/3790)
- Add section on record type positionals and more examples with function types. Discussion: [language/#3791](https://github.com/dart-lang/language/issues/3791)

### 1.2

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

0 comments on commit 25a9cb3

Please sign in to comment.