Skip to content

Commit

Permalink
format examples
Browse files Browse the repository at this point in the history
  • Loading branch information
MaryaBelanger committed Jan 2, 2025
1 parent 406d2b9 commit 9f1d115
Showing 1 changed file with 48 additions and 40 deletions.
88 changes: 48 additions & 40 deletions src/content/language/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,51 +290,59 @@ cannot be changed: they're _immutable_.
For more information on using `const` to create constant values, see
[Lists][], [Maps][], and [Classes][].

## Wildcard variables

A wildcard variable declares a local variable or parameter that is non-binding;
essentially, a placeholder.
You can declare a wildcard variable using the name `_`.

Because it does not bind to anything, you can have multiple declarations named
`_` in the same namespace without a collision error.

```dart
// local variable declaration
main() {
var _ = 1;
int _ = 2;
}

// for loop varibale declartaion
for (var _ in list) {}
// catch clause parameters
try {
throw '!';
} catch (_) {
print('oops');
}
// generic type and function type parameters
class T<_> {}
void genericFunction<_>() {}
takeGenericCallback(<_>() => true);
// function parameters
Foo(_, this._, super._, void _()) {}
list.where((_) => true);
void f(void g(int _, bool _)) {}
## Wildcard variables

typedef T = void Function(String _, String _);
```
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 is not accessible.
Multiple declarations named `_` can exist in the same namespace without a collision error.

Any declarations local to a block scope is a valid use for wildcard variables.
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 varibale 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 _);
```


[Assert]: /language/error-handling#assert
Expand Down

0 comments on commit 9f1d115

Please sign in to comment.