-
Notifications
You must be signed in to change notification settings - Fork 228
Description
Admin comment by @mit-mit:
This proposal now has a feature specification here: super parameters.
This feature enables not having to repeat all parameters from a super class twice. Code previously written like:
class B {
final int foo;
final int bar;
final int baz;
B(this.foo, this.bar, [this.baz = 4]);
}
class C extends B {
C(super.foo, super.bar, [super.baz = 4]) : super(foo, bar, baz);
}can now be written as:
class C extends B {
C(super.foo, super.bar, [super.baz = 4]);
}This is likely especially useful is code like Flutter widgets, see for example RaisedButton.
Original comment by @roy-sianez:
Currently, in Dart, there is syntactic sugar to initialize the property of a class from a constructor parameter:
// Example 1
class Person {
String name;
Person(this.name); // A convenient constructor
}However, if you want to initialize the property of a superclass, you have to write boilerplate code:
// Example 2
class Employee extends Person {
String department;
Employee(String name, this.department) :
super(name); // Boilerplate
}I propose adding syntactic sugar where super could be used instead of this in the context of a constructor parameter to initialize the property of a superclass, like so:
// Example 3
class Employee extends Person {
String department;
Employee(super.name, this.department); // Easier to read and write
}Example 3 would be equivalent to example 2.
To prevent the bypassing of important logic in superclass constructors, it could be a requirement that to use this syntactic sugar, the superclass must provide a constructor that uses all this. or super. parameters; a class that uses super. syntactic sugar would delegate to this constructor in its superclass.
This syntactic sugar would be most useful for simple classes where fields are initialized without much preprocessing or logic.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status