dev_compiler
dev_compiler copied to clipboard
Field initializers not run in presence of initializing constructor params
class Bar {
Bar() {
print("Bar created");
}
}
class Foo {
var bar = new Bar();
Foo([this.bar]);
}
main() {
print(new Foo().bar);
}
Dart prints:
Bar created
null
But ddc just does not create the unused Bar instance at all.
possibly we just want to make this an error, as it doesn't seem especially useful, but not sure what others think.
related #212
Clearly defining field values that aren't used is a code smell (especially if creating that value has side-effects), but I guess it's not completely unlikely to happen in the presence of multiple constructors:
class Foo {
var bar = new Bar();
Foo();
Foo.withCustomBar(this.bar) { /* some different behaviour */ }
}