dev_compiler icon indicating copy to clipboard operation
dev_compiler copied to clipboard

Field initializers not run in presence of initializing constructor params

Open ochafik opened this issue 10 years ago • 3 comments

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.

ochafik avatar Jun 10 '15 15:06 ochafik

possibly we just want to make this an error, as it doesn't seem especially useful, but not sure what others think.

jmesserly avatar Jun 10 '15 15:06 jmesserly

related #212

jmesserly avatar Jun 10 '15 15:06 jmesserly

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 */ }
}

ochafik avatar Jun 10 '15 15:06 ochafik