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

Make accessing "this" in initializers a runtime error, not a static error. #12352

Closed
justinfagnani opened this issue Aug 9, 2013 · 3 comments
Labels
area-language New language issues should be filed at https://github.com/dart-lang/language closed-duplicate Closed in favor of an existing report type-enhancement A request for a change that isn't a bug

Comments

@justinfagnani
Copy link
Contributor

This would allow "this" references in closures declared in initializers that aren't invoked until after the instance is fully initialized. This would be very convenient for

* Default values for function-valued fields:

class A {
  var x;
  var reset = () { x = 0; };
}

* Similarly, closures used in declarative configuration of objects, in the style that's popular with JS frameworks like Ember, Angular, etc.:

class MyApp extends App {
  var section;
  final router = new Router({
    'home': route('/')..onEnter.listen((e) => section = 'home');
    'one': route('/page_one')..onEnter.listen((e) => section = 'one');
  });
}

Of course the closures could be executed before the object is initialized, but in many cases they just capture the 'this' reference and access it later. By not allow 'this' references in initializers at all we force some initialization to happen in the constructor body and thus limit the use of final in these cases.

@justinfagnani
Copy link
Contributor Author

Removed Type-Defect label.
Added Type-Enhancement label.

@gbracha
Copy link
Contributor

gbracha commented Aug 9, 2013

I agree the situation we have in unfortunate. Not sure what rules would characterize the legitimate cases.
Wacky idea: referencing this is allowed in a closure inside an initializer but accessing it results in runtime error until the initializer completes. This might be done by changing the context of the closure at the end of initialization.


Added Accepted label.

@justinfagnani justinfagnani added Type-Enhancement area-language New language issues should be filed at https://github.com/dart-lang/language labels Aug 9, 2013
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed priority-unassigned labels Feb 29, 2016
@natebosch
Copy link
Member

Resolving as a duplicate of #33857 because that issue has more active discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language New language issues should be filed at https://github.com/dart-lang/language closed-duplicate Closed in favor of an existing report type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants