Skip to content

Pattern destructuring could enable calling FunctionTypes #4234

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
FMorschel opened this issue Jan 23, 2025 · 3 comments
Closed

Pattern destructuring could enable calling FunctionTypes #4234

FMorschel opened this issue Jan 23, 2025 · 3 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@FMorschel
Copy link

Today we have patterns that allow us to deconstruct a variable:

class Class {
  int myField = 0;
}

void foo() {
  var instance = Class();
  if (instance case Class(myField: var myFieldValue)) {}
}

I think of this structure like:

//    getter assigns to variable
Class(myField: var myFieldValue)

Basically the = operator but swapped:

var myFieldValue = instance.myField;

We also can get a method as a FunctionType:

class Class {
  int foo() => 0;
}

void foo() {
  var instance = Class();
  if (instance case Class(foo: var fooFn)) {
    fooFn(3);
  }
}

I'd like to request a change in that we could:

if (instance case Class(foo(3): var fooValue)) {

And for Future we could add await before var like:

class Class {
  Future<int> foo() async => 0;
}

void foo() async {
  var instance = Class();
  if (instance case Class(foo(3): await var fooValue)) {
    print(fooValue);
  }
}
@FMorschel FMorschel added the feature Proposed language feature that solves one or more problems label Jan 23, 2025
@FMorschel
Copy link
Author

I don't expect this to be used on methods that contain many arguments since that would become harder to read, but I feel like this could help with small ones avoing another line.

@lrhn
Copy link
Member

lrhn commented Jan 24, 2025

We also can get a method as a FunctionType:

Not absolutely sure that's intended, but will have to check the future specification to be sure whether it's allowed by that.

Edit: I checked, it's intended to work.
Don't know why, but it's there.

I'd like to request a change in that we could:

if (instance case Class(foo(3): var fooValue)) {

So would I: #2433

For allowing await, combine that with #25

@munificent
Copy link
Member

If I understand the proposal here correctly, I think it's pretty much a duplicate of #2433, so I'm going to go ahead and close this one. If I'm wrong, let me know and we can reopen it.

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
None yet
Development

No branches or pull requests

3 participants