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

Constructor of subclass is incredibly verbose #36978

Closed
MilesAdamson opened this issue May 15, 2019 · 1 comment
Closed

Constructor of subclass is incredibly verbose #36978

MilesAdamson opened this issue May 15, 2019 · 1 comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). type-enhancement A request for a change that isn't a bug

Comments

@MilesAdamson
Copy link

Issue when invoking super of parent class. You have to write out every viable twice, once in the child constructor and then again inside super(). This is very verbose for classes with many variables. A constructor I just wrote at work was 32 lines.

But if I had syntax to say "you must pass every variable the super class needs" it would be only 1 line.

void main() {
     var child = Child(one: 1, two: 2, three: '3', childVar: 4);  
     print(child.childVar);
}

class Parent {
    final varOne;
    final varTwo;
    final varThree;
  Parent({this.varOne, this.varTwo, this.varThree});
}

class Child extends Parent {

  final childVar;

  // No way to implicitly require/pass everything for super
  Child(
      {int one,
      int two,
      String three,
      this.childVar})
        : super(
          varOne: one,
          varTwo: two,
          varThree: three);
}

I would like to be able to do something like this:

  // You must pass in every variable the super class requires
  Child({this.childVar}) : super(...);
@MilesAdamson MilesAdamson changed the title Constructor of extended class is incredibly verbose Constructor of subclass class is incredibly verbose May 15, 2019
@MilesAdamson MilesAdamson changed the title Constructor of subclass class is incredibly verbose Constructor of subclass is incredibly verbose May 15, 2019
@lrhn lrhn added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). type-enhancement A request for a change that isn't a bug labels May 16, 2019
@Levi-Lesches
Copy link

See dart-lang/language#493 (comment), which proposes:

void main() {
  var child = Child(one: 1, two: 2, three: '3', childVar: 4);  
  print(child.childVar);
}

class Parent {
  final varOne;
  final varTwo;
  final varThree;
  Parent({this.varOne, this.varTwo, this.varThree});
}

class Child extends Parent {
  final childVar;

  Child({this.childVar, super.varOne, super.varTwo, super.varThree});
}

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). type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

3 participants