Skip to content

Pattern destructuring could enable calling FunctionTypes #4234

Open
@FMorschel

Description

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);
  }
}

Metadata

Assignees

No one assigned

    Labels

    featureProposed language feature that solves one or more problems

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions