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

Add wildcard variables #6295

Merged
merged 7 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions src/content/language/libraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ Element element1 = Element();
lib2.Element element2 = lib2.Element();
```

Import prefixes with the [wildcard][] name `_` are non-binding,
but will provide access to the non-private extensions in that library.

[wildcard]: /language/variables#wildcard-variables

### Importing only part of a library

If you want to use only part of a library, you can selectively import
Expand Down
66 changes: 66 additions & 0 deletions src/content/language/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,70 @@ For more information on using `const` to create constant values, see
[Lists][], [Maps][], and [Classes][].


## Wildcard variables
MaryaBelanger marked this conversation as resolved.
Show resolved Hide resolved

:::version-note
Wildcard variables require
a [language version][] of at least 3.7.
MaryaBelanger marked this conversation as resolved.
Show resolved Hide resolved
:::

A wildcard variable with the name `_` declares a local variable or parameter
that is non-binding; essentially, a placeholder.
The initializer, if there is one, is still executed, but the value isn't accessible.
Multiple declarations named `_` can exist in the same namespace without a collision error.

Top-level declarations or members where library privacy might be affected are
not valid uses for wildcard variables.
Declarations local to a block scope, such as the following examples,
can declare a wildcard:

* Local variable declaration.
```dart
main() {
var _ = 1;
int _ = 2;
}
```

* For loop variable declartaion.
```dart
for (var _ in list) {}
```

* Catch clause parameters.
```dart
try {
throw '!';
} catch (_) {
print('oops');
}
```

* Generic type and function type parameters.
```dart
class T<_> {}
void genericFunction<_>() {}

takeGenericCallback(<_>() => true);
```

* Function parameters.
```dart
Foo(_, this._, super._, void _()) {}

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

void f(void g(int _, bool _)) {}

typedef T = void Function(String _, String _);
```

:::tip
Enable the lint [`unnecessary_underscores`][] to identify where a single
non-binding wildcard variable `_` can replace the previous convention of using
multiple binding underscores (`__`,`___`, etc.) to avoid name collisions.
:::

[Assert]: /language/error-handling#assert
[Instance variables]: /language/classes#instance-variables
[DON'T use const redundantly]: /effective-dart/usage#dont-use-const-redundantly
Expand All @@ -300,3 +364,5 @@ For more information on using `const` to create constant values, see
[Lists]: /language/collections#lists
[Maps]: /language/collections#maps
[Classes]: /language/classes
[language version]: /resources/language/evolution#language-versioning
[`unnecessary_underscores`]: /tools/linter-rules/unnecessary_underscores
Loading