-
Notifications
You must be signed in to change notification settings - Fork 205
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
Enhancing Flexibility in Method Overrides #4138
Comments
I think this is, essentially, asking for the same named parameter to be able to have more than one name. Dart functions exist independtly of their declaration. It doesn't matter what we write at the declaration, it matters what the runtime type of var c = (Range<int>(lower: 42, upper: 87) as Pair<int, int>).copyWith; is. Since the static type is var range = Range<int>(lower: 42, upper: 87);
var pair = range as Pair<int, int>;
var c = pair.copyWith; // Static type `Pair<int, int> Function({int? first, int? last})`.
var d = range.copyWith; // static and runtime type ... ???
assert(identical(c, d)); Whatever runtime type That's ... probably possible. Won't say easy or likely, but I can see a consistent underlying model. class Range ... {
Range<T> copyWith({T? lower + first, T? upper + second}) => ...;
}
void main() {
Range<int> Function({T? lower + first, R? upper + second}) copy = Range<int>(lower: 4, upper: 8).copyWith;
} (Alternative operators could be Every named parameter, in functions and function types, can have names which is Function subtyping allows a subtype to have more names for a named parameter than the supertype. That is, if the supertype has a named parameter with names n1,...,nk, the subtype must have a named parameter with names that include all of n1,...,nk, but it may add more names to that parameter. A function invocation can pass an argument only once, by any of its names. It's an error to do, fx, Inside the function you can either only access the parameter by its first name, or by all the names. (Which means it's not symmetric, so the Need to consider (non-redirecting generative) constructors. class C extends B {
int x;
C({this.x + a = 0, super.y + b = 0}) : assert(x >= 0);
} This could be allowed, calling it as I don't see this feature happening. It's nice in that it allows you to use different names for named parametes in a subclass, but it doesn't remove the old name, so it's likely to feel cluttered. It complicates function types in general. Here I'd likely use an extension type instead of a subtype: extension type Range<T extends Comparable>._(Pair<T?, T?> _) /* implements Pair<T?, T?> */ {
Range({T? lower, T? upper}) : this._(Pair<T?, T?>(lower, upper));
T? get lower => _.first;
T? get upper => _.second;
Range<T> copyWith({T? lower, T? upper}) => Range<T>(
lower: lower ?? this.lower,
upper: upper ?? this.upper,
);
} You can add the |
Dart's strict method signature matching for overrides limits subclasses from using meaningful parameter names. This impacts methods clarity and usability, leading to unnecessary boilerplate.
In this example, the subclass
Range
ideally wants to use parameters likelower
andupper
to represent the bounds of the range, but is forced to stick withfirst
andsecond
to avoid signature mismatches. Introducing another method likecopyWithRange
becomes redundant and inefficient.Solution Proposal:
Dart should allow parameter renaming in method overrides, maintaining type compatibility with the base class. This would enhance clarity without breaking contracts.
Possible Syntax:
In this solution, the
lower
andupper
parameters are mapped to the base class'sfirst
andsecond
, allowing more meaningful parameter names in the subclass without breaking the base class contract.The text was updated successfully, but these errors were encountered: