Skip to content

Destructuring of records doesn't work in lambda parameters #54182

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

Closed
lukehutch opened this issue Nov 29, 2023 · 3 comments
Closed

Destructuring of records doesn't work in lambda parameters #54182

lukehutch opened this issue Nov 29, 2023 · 3 comments

Comments

@lukehutch
Copy link

lukehutch commented Nov 29, 2023

This works:

List<(int, int)> vals = [];
for (var (a, b) in vals) {
  print(a+b);
}

but this fails with error The argument type 'InvalidType Function(dynamic Function(int, int))' can't be assigned to the parameter type 'dynamic Function((int, int))':

List<(int, int)> vals = [];
final sums = vals.map(((int a, int b)) => a + b).toList();  // Error

I assumed destructuring should work in lambda parameters.

Actually it doesn't work even in function definitions -- this fails with a very strange syntax error, Use the generic function type syntax to declare the parameter ''. Try using the generic function type syntax.

void sum((int a, int b)) => a + b;  // Error

The following works (by not using destructuring at all). You can't use v.a or v.b, since the field names are ignored in the type:

void sum((int a, int b) v) => v.$1 + v.$2;
@mraleph
Copy link
Member

mraleph commented Nov 29, 2023

Duplicate of dart-lang/language#3001

(On which you have already commented few weeks ago by the way. So no need to file new issues).

@mraleph mraleph closed this as completed Nov 29, 2023
@lukehutch
Copy link
Author

Face-palm :-D Thanks for pointing that out, I totally forgot that I found that issue. I went looking for it again, and didn't find it this time...

Before I go file another dup, is there an issue for allowing v.a and v.b to be used instead of v.$1 and v.$2, in the last example I gave?

@mraleph
Copy link
Member

mraleph commented Nov 29, 2023

is there an issue for allowing v.a and v.b to be used instead of v.$1 and v.$2, in the last example I gave?

I don't think so. Please file it at dart-lang/language repo (that's the place for all language feature requests).

The choice of names not having any meaning was deliberate, but I don't see any reason why it would not work to just allow this in a statically typed context (i.e. ignoring dynamic invocations entirely).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants