Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wildcard-variables] Add more explicit details on functions and records. #3802

Merged
merged 3 commits into from
May 15, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 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
kallentu marked this conversation as resolved.
Show resolved Hide resolved

A *local declaration* is any of:
kallentu marked this conversation as resolved.
Show resolved Hide resolved

* 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 _(), {_}) {}
kallentu marked this conversation as resolved.
Show resolved Hide resolved

list.where((_) => true);

void f(void g(_, _)) {}
kallentu marked this conversation as resolved.
Show resolved Hide resolved

typedef T = void Function(String _, String _);
kallentu marked this conversation as resolved.
Show resolved Hide resolved
```

* Local variable declaration statement variables.
Expand Down Expand Up @@ -127,8 +135,31 @@ 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;
```

Record type field names will no longer emit an `INVALID_FIELD_NAME` warning.
kallentu marked this conversation as resolved.
Show resolved Hide resolved
This will have no other effect since the names of positional record fields
don't matter.

### Local function declarations

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

It's an error to declare a local function declaration named `_`.
kallentu marked this conversation as resolved.
Show resolved Hide resolved

### Other declarations
kallentu marked this conversation as resolved.
Show resolved Hide resolved
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 +434,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