diff --git a/src/content/language/variables.md b/src/content/language/variables.md index dfe4597a08..f21d138c98 100644 --- a/src/content/language/variables.md +++ b/src/content/language/variables.md @@ -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