Skip to content

Pattern destructing could assign values to existing variables #4235

@FMorschel

Description

@FMorschel

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

This only creates new variables after the :. I'd like to request a way for us to assign this to an existing variable. This could only work when we have this outside if/for lines, like:

late int myValue1;
late int myValue2;
late int myValue3;
if (test) {
  Class(myField := myValue1, otherField := myValue2, thirdField := myValue3) = instance;
} else {
  myValue1 = 0;
  myValue2 = 0;
  myValue3 = 0;
}

The := is only a representation here, I'm not certain this is the best solution. This would allow us to avoid creating multiple lines here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    requestRequests to resolve a particular developer problem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions