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

Destructuring assignment in initializer lists. #2774

Open
lrhn opened this issue Jan 15, 2023 · 1 comment
Open

Destructuring assignment in initializer lists. #2774

lrhn opened this issue Jan 15, 2023 · 1 comment
Labels
patterns Issues related to pattern matching.

Comments

@lrhn
Copy link
Member

lrhn commented Jan 15, 2023

One more place where a variable is "assigned" is in initializer lists.
Example:

class Coordinates {
  final int x, y;
  Coordinates.fromPoint((int, int) point) : this.x = point.$1, this.y = point.$2;
}

This is also a place where a destructuring assignment makes sense. It would be nice to be able to write the above as:

  Coordinates.fromPoint((int, int) point) : (this.x, this.y) = point;

Apart from convenience, allowing the value of one computation to flow into multiple field initializations is something that requires using multiple chained constructors today.
It would allow something like:

   MyAsyncClass() : (_controller, stream) = let c = StreamController<T>() in (c, c.stream);

(where let is from #2703, without that I can get something similar with suitable extension methods).

This is not about patterns in parameters, which we should also have, and which might then apply to initializing formals, like:

  Coordinates((this.x, this.y));

The destructuring assignment in the initializer list is more general, because it applies not only to values that are arguments.

@lrhn
Copy link
Member Author

lrhn commented Apr 17, 2023

(If we don't want to add one more incremental feature to the limited syntax of initializer lists, an alternative is to move away from them, by either having an entire initializer block, or have an initializer section of the constructor body, where arbitrary statements are allowed, and only this access is restricted. That's a larger feature, but it opens up for much more expressive power during initialization, so you'd no longer have to use an intermediate redirecting or factory constructor just to introduce a local variable. See #2197.)

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

No branches or pull requests

2 participants