-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Make it easier to add new final fields to existing constructors #53707
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
(unsure if this is really a request for a refactoring or a quick-fix) |
I can see how that would be useful. The reason we put the diagnostic on the constructors rather than the field is that every constructor needs to be looked at to decide the right way to add the initialization. I think the same reasoning applies to a fix. Maybe it's the case in practice that either (a) there is typically a single constructor, so the options could be presented at the field declaration, or (b) one of the fixes, such as adding a parameter, is common enough that having a way to apply it to all constructors would be useful. I don't know whether either of those is the case, but we could certainly consider adding this kind of support for those special cases; I don't think we'd want to add every combination of edit x constructor.
The same consideration would apply for an "add field" refactoring. |
My thinking was that the same two options from the first screenshot would be useful, and they're added to all constructors that have a diagnostic because this field is missing:
|
Just to point out the similar interest with #45821 |
Here is a thing which is somewhat relevant: If we add support for primary constructors (something like this proposal) and inferred // First version.
class const A({String a, String b});
// Second version.
class const A({String a, String b, String c}); Of course, this isn't a solution to the original request because it wouldn't do anything to fix all the other constructors (if any). However, it would make the transition cost lower in some cases that might be common. It might even be useful to use this together with a refactoring that transforms a regular constructor to a primary constructor, or vice versa. |
I also think this could this be the same as #53301. |
Just a full review for us to reference. Coming from #54156 (comment): I've tested some combinations of fixes. Things I noticed: class C {
final int a;
C({required this.a});
}
class D extends C {
final int b;
}
Note: If Some things do change with widgets: class MyWidget extends StatelessWidget {
final String text;
@override
Widget build(BuildContext context) => const SizedBox();
}
|
Here is a CL to fix 3.b above https://dart-review.googlesource.com/c/sdk/+/422440. Here is a CL to fix @bwilkerson, what are your takes on 1 and 3?
Also consider the second case for
And 3:
I'd personally say both could generate |
Bug: #53707 Change-Id: I461b2a74ae4d8a9b1794b1e47574783f31d29a67 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/422440 Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]> Auto-Submit: Felipe Morschel <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
This code:
produces diagnostics for
field_not_initialized
and has a convenient fix to add a constructor:If I use that fix and later add a new field
c
:There is no diagnostic on
c
, nor are there any code actions to add C to the constructor (from the declaration ofc
, where I was typing). Instead, there is a fix on the constructor:This is less convenient because it requires moving to the constructor and there's no option to add it as a named field (you have to use "Add final field formal parameters" and then "Convert selected formal parameter(s) to named" or "Convert all formal parameters to name" (although that one doesn't move it to the end where you might prefer it)).
It would be useful if a field that is not assigned in a constructor (causing
final_not_initialized_constructor
) provided a fix/assist at the location of the field (with both named and positional options).(@bwilkerson one thing I thought about here was having context messages on
final_not_initialized_constructor
that point at the field, and showing fixes against the locations of context messages in addition to where the diagnostic is rendered... although it may fall down when there are two constructors with this diagnostic pointing at the same field as that would result in two fixes on the field declaration)The text was updated successfully, but these errors were encountered: