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

Provide access to previous constructor initializers from later ones #622

Open
nex3 opened this issue Oct 11, 2019 · 2 comments
Open

Provide access to previous constructor initializers from later ones #622

nex3 opened this issue Oct 11, 2019 · 2 comments
Labels
request Requests to resolve a particular developer problem

Comments

@nex3
Copy link
Member

nex3 commented Oct 11, 2019

Today in a constructor, I can refer to fields initialized with this.-style parameters:

Foo(this._value)
  : _computed = _value + 1; // This works! 🎉

As I understand it, this works by creating a local variable _value that shadows the field and has the same (initial) value. However, if you want to access an explicit initializer's value from a later initializer, you're out of luck:

Foo(this._value)
  : _computed = _value + 1,
    _recomputed = _computed + 1; // This doesn't work! 😢

I propose that each initializer creates a local variable that's visible for the initializer expressions that lexically follow it, and that shadows the corresponding instance variable name just like this. parameters. This would be very useful when doing moderately complex pre-computations in constructors, and it would help avoid unnecessary factory constructors or late fields.

@nex3
Copy link
Member Author

nex3 commented Dec 12, 2023

I'm dying for this in some code I'm writing right now. I could work around it with late final, but then I have to add a bunch of fields to my class for what is essentially just intermediate data. The next best option is duplicating the entire constructor into a private constructor, which adds a ton of extra code for no benefit.

@lrhn
Copy link
Member

lrhn commented Dec 14, 2023

I'd actually rather have "declaration expressions" (#1420), an expression introducing declarations into the local scope, because it is more general if one of those could survive from one initializer list entry to the next.

Consider:

 Constructor() : sink = (var controller = StreamController<int>()).sink, stream = controller.stream;

where the value I want to reuse is not the one stored into a variable.

Could do something similar by allowing local variable declarations in the initializer list:

 Constructor() : var controller = StreamController<int>(), sink = controller.sink, stream = controller.stream;

but that's more specialized, doesn't help in other expressions, and if we're going in that direction, we might as well a full statement block as initializer, like #3002.)

But this feature is also reasonable, and simpler, so we could have both. We should have both. 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
request Requests to resolve a particular developer problem
Projects
None yet
Development

No branches or pull requests

2 participants