Make accessing "this" in initializers a runtime error, not a static error. #12352
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
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.
The text was updated successfully, but these errors were encountered: