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

Failed to assign to final instance variable in constructor #427

Closed
DartBot opened this issue Nov 11, 2011 · 4 comments
Closed

Failed to assign to final instance variable in constructor #427

DartBot opened this issue Nov 11, 2011 · 4 comments
Assignees
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-not-planned Closed as we don't intend to take action on the reported issue type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Nov 11, 2011

This issue was originally filed by [email protected]


I believe this should be legal:

class Object {
  Object(this._foo) {
    _bar = _foo.bar;
  }

  final _foo;
  final _bar;
}

However I get the error 'cannot assign value to final variable _bar'. I can't put the assignment in the initializer list as the assignment refers to _foo.

As it is, the only solution I've found is to make _bar non-final which is less than ideal.

@DartBot
Copy link
Author

DartBot commented Nov 11, 2011

This comment was originally written by [email protected]


You can put both the assignments into the initializer list, can't you?

class Object {
  Object(foo) : _foo = foo, _bar = foo.bar;

  final _foo;
  final _bar;
}

Slightly suboptimal (I love the shorthand syntax Obj(this._foo) in constructor arguments), but not exactly bad.

@DartBot
Copy link
Author

DartBot commented Nov 11, 2011

This comment was originally written by [email protected]


Apparently I can, yes. It's a shame there's an inconsistency there, but
it's a limitation I can live with :)

Thanks!

@DartBot
Copy link
Author

DartBot commented Nov 11, 2011

This comment was originally written by [email protected]


Removed Type-Defect label.
Added Type-Enhancement, Area-Language, Triaged labels.

@gbracha
Copy link
Contributor

gbracha commented Dec 14, 2011

There's no inconsistency. We require final fields to be initialized in the initializer list, so we can ensure they get initialized. We do not allow access to 'this' on the right hand side of assignmenyts in the initializer list, so we don't have dependencies on virtual method calls and uninitialized fields etc. Hence the need to initialize _bar in the initializer list using a parameter rather than _foo.

The this.x initializing parameter sugar is for the common case where the only use of the parameter is to initialize a field. The parameter is not in scope in the constructor body or initializer list. In more complex cases, you live without the sugar.


Set owner to @gbracha.
Added WontFix label.

@DartBot DartBot added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Dec 14, 2011
@kevmoo kevmoo added closed-not-planned Closed as we don't intend to take action on the reported issue type-enhancement A request for a change that isn't a bug and removed resolution-wont_fix labels Mar 1, 2016
nex3 pushed a commit that referenced this issue Aug 31, 2016
mainly, this backs off of some readability optimizations around static and top-level fields that were too aggressive. On their own, they were okay, but they collide with the library-cycle issues. Once we can remove that issue, we could consider restoring some of this. In the meantime, simplicity is good.

The new operation in the declaration loader is to allow us to see if an initializer has all its dependencies satisfied, but without changing any ordering if they are not.

[email protected]

Review URL: https://codereview.chromium.org/1636233002 .
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-not-planned Closed as we don't intend to take action on the reported issue type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

3 participants