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 #3712

Open
munificent opened this issue Apr 18, 2024 · 1 comment
Open

Wildcard variables #3712

munificent opened this issue Apr 18, 2024 · 1 comment
Assignees
Labels
feature Proposed language feature that solves one or more problems

Comments

@munificent
Copy link
Member

Pattern matching brought a new way to declare variables. Inside patterns, any variable whose name is _ is considered a "wildcard". It behaves like a variable syntactically, but doesn't actually create a variable with that name. That means you can use _ multiple times in a pattern without a name collision.

That leads to an irregularity:

var (_, _) = (1, 2); // OK.

var _ = 1;
var _ = 2; // Error. Already a variable in scope with this name.

Also, it's annoying that _ binds a name. When you have a lambda with more than one parameter that you don't use, you end up having to do:

takesCallback((_, __, ___) { ... });

I have a proposal to fix both by saying that local variables and parameters named _ are also non-binding. This is the tracking issue for that proposal.

@mit-mit mit-mit added this to Being discussed in Language funnel Apr 18, 2024
@mit-mit mit-mit added the feature Proposed language feature that solves one or more problems label Apr 18, 2024
@Mike278
Copy link

Mike278 commented Apr 18, 2024

I always love seeing those usage-pattern breakdowns across huge corpuses of Dart code!

From the proposal:

From skimming some of the examples, it does look like some users sometimes name lambda parameters _ and then use the parameter in the body.

As one of those users I just wanted to add that my two use cases for doing this are to workaround #8 (unless there's a better issue to reference?)

xs.map((_) => _.isEven);
// vs
xs.map(.isEven); // strawman syntax

and #3001:

users.map((_) {
  final (user, selected) = _;
  ...
})
// vs
users.map((user, selected) {
  ...
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
Language funnel
Being spec'ed
Development

No branches or pull requests

4 participants