-
Notifications
You must be signed in to change notification settings - Fork 213
[Feature] Not require a default value when arguments are given the default value from it's super class #2423
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
If override methods cannot set a default value, it would be a breaking change, which makes the super arguments better for adoptions. |
First note that there is no choice in Kotlin, because the default value is provided at the call site by the compiler (so if the invoked method In Dart, default values are provided as specified in the actual callee. class A {
void m([int i = 42]) {}
}
class B implements A {
void m([int i = 43]) {}
}
void main() {
A a = B();
a.m(); // Will pass 43.
dynamic d = a;
d.m(); // Will pass 43.
} The Dart design enables overriding relations to be more flexible. For instance, we may not want to use the same default value in all subclasses: abstract class A<X> {
void m([X x]);
}
class Aint implements A<int> {
void m([int i = 42]) {/*...*/}
}
class AString implements A<String> {
void m([String s = 'Hello!']) {/*...*/}
}
class AOther<X> implements A<X> {
void m([X? x]) {/*...*/} // This is an OK override, using null as the default value.
} In this situation it is impossible to specify a particular default value for the parameter of The examples illustrate that we may be able to choose a suitable default value in some cases, and we can override the parameter type to be nullable in order to handle other cases (the implementation of Another place where it makes a difference is that Dart function types allow for optional parameters to be eliminated by subsumption (that is, a supertype can forget about some or all optional parameters): void f(int i, {bool b = false, required double d}) {
print(b);
}
void main() {
void Function(int, {required double d}) g = f; // OK.
f(42, d: 0.01); // Prints 'false'.
} This illustrates that the Dart design relies on having default values that are provided as part of the abstraction itself, not as syntactic sugar for adding some extra arguments at each call site, and also that it is a choice that provides additional expressive power in some ways. Dart actually used to have a warning at compile time for the situation where an instance method declaration Returning to the question about how to express the overriding declaration in the case where we want to reuse the same default value, take a look at the feature proposal in #2269. We could then have this: class A {
void test({String test = 'default'}) {}
}
class C extends A {
@override
void test({String test = default}) {}
}
|
@eernstg Thanks for the great explanation. I think we can close this since the corresponding proposal already addressed this. 👍 |
I didn't find any related issues, please link to this if any.
Consider the code:
The above code will always report an error in the null-safety world (not sure if it's the same in non-null-safety). To avoid the error, we might turn the code into this to avoid redundant hard-code content:
However, in Kotlin, the default value cannot be re-defined if it's overriding a method:
I think it can be useful for Dart to handle this, or we can make arguments as super arguments (but
super
here seems in a bad semantic condition):The text was updated successfully, but these errors were encountered: