-
Notifications
You must be signed in to change notification settings - Fork 213
[Feature Request] Can we have syntactic sugar for calling super constructors? #370
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
Comments
What happened to |
I forgot to put it in. 😄 I'll edit it |
There's a similar issue here: #57 |
The behavior of such a short-hand should be predictable. TextChatMessage([super.timestamp, super.senderId, super.body} : super(messageType: ...); it's fairly easy to define the behavior because the order of named arguments are unimportant. We can just append the super-arguments to get: TextChatMessage([super.timestamp, super.senderId, super.body} : super(messageType: ...,
timestamp: timestamp, senderId senderId, body: body); It gets a little more complicated with positional parameters. Should the If appended: Gizmo(String name, super.price, super.cost) : super("Gizmo: $name"); would become Gizmo(String name, super.price, super.cost) : super("Gizmo: $name", price, cost); If prepended, it would become: Gizmo(String name, super.price, super.cost) : super(price, cost, "Gizmo: $name"); Either makes sense. I'd say prepending is probably the best choice. Then a sub-class constructor would start with the same arguments as the super-class constructor, and extend it with more details. Named parameters are easy :) If you just want to forward all super-class parameters, then perhaps you can do: final String damageType;
Gizmo(super, this.damageType); and then |
From that document:
Specifically,
To clarify, is this what's being referred to? class A {
int x;
bool z;
A.foo(this.x, {required this.z});
}
class B extends A {
String y;
/// override A.x
@override
int get x => super.x + 1;
// B(super.x, this.y, {required super.z}) : super.foo() {
// something(x, y, z);
// }
//
// Translates to:
B(x, this.y, {required z}) : super.foo(x, z: z) {
something(x); // the parameter of the constructor
something(this.x); // the getter
}
void something(int a) => print(a);
}
void main() {
A b = B(42, "Hello, World!", z: true); // prints 42 then 43
} |
No the point is that initializing formals ( Cf. language/specification/dartLangSpec.tex Line 3612 in 0f77128
|
Isn't this already implemented? |
It is! |
To make calling super constructors more beautiful, can we have this syntactic sugar?
Instead of:
I would like to be able to write:
One could say "well you're still using the
super()
syntax so what's the difference?"To that I'd say true, but using
this.messageType
in constructors is also syntactic sugar, but not a requirement either.The text was updated successfully, but these errors were encountered: