-
Notifications
You must be signed in to change notification settings - Fork 1.7k
add "destructure variable declaration" assist(s) #52025
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
I'm not convinced that the non-record cases carry their weight, but I do agree that an assist to destructure a record is worth considering. If we add this, we should focus on records for a first cut and then consider the others as enhancements to the base functionality. |
/fyi @leafpetersen |
First, basic cut. See: #52025 Change-Id: I3388f5ec96a45211669e3ac8a5975ff39feea4bb Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/295381 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Phil Quitslund <pquitslund@google.com>
Basic records support landed w/ cae0bfa. There's an open question of whether a field name suggestion should be a linked edit (https://dart-review.googlesource.com/c/sdk/+/295381/comment/6c626001_ad74fd62/) which I'll return to. Also, I wonder if we should add wildcards as additional suggestions for positional fields? That is, we might add |
I like the idea of offering a wildcard pattern here, it's a reasonable choice. We should also consider suggesting wildcard patterns in code completion. |
I like this idea, especially for record and object patterns. I'm not quite as convinced by the map and list use cases. Is there a way that we could make it available even if there's no pre-existing variable declaration? I.e. if I have: class A {
int a;
A(this.a);
}
f() => A(1);
m() {
f();
} it would be nice if I could put the cursor on the |
Currently the expectation is that you'd do this in two steps, introducing the local variable first. I do like the idea of supporting this in one go though. Maybe we could add something like "Assign value to a new destructured pattern variable" to the already existing assist? (Word-smithing welcome.) (FWIW @bwilkerson, @scheglov: this is another example of micro vs. macro edits as discussed earlier today.) |
After some thinking and an informal poll of folks, I'd like to go ahead with a simple conversion for Object patterns that takes class A {
int a;
A(this.a);
}
f() => A(1);
m() {
var o = f();
} and produces class A {
int a;
A(this.a);
}
f() => A(1);
m() {
var A() = f();
} with the caret inside the new declaration ( |
See: #52025 Change-Id: I298c7f5b82b47b8cb02ca1a401e0658aab8f40a3 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/295680 Commit-Queue: Phil Quitslund <pquitslund@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
See: #52025 Change-Id: I6dc5d2f0c8391d8ad719c23b47aadeac9a8ab969 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/295722 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Phil Quitslund <pquitslund@google.com>
Particularly for records, a destructuring assist would be handy and I expect commonly used but it would be interesting to explore lists, maps and objects too.
Records
For example,
could become
And,
might become:
It gets interesting if the record declaration is accessed.
For example, I'd expect no assist in this case:
but,
could reasonably become:
Maps
could become
(But note, they'll need to do something to
{}
since map patterns can't be empty.)Lists
could become
(Or
var [...] = f();
)Objects
In a simple case we might take something like
to:
but this doesn't scale if
A
has a lot of fields.Maybe better to start w/
var A() = f();
and possibly go better if fields are accessed.
could become
The text was updated successfully, but these errors were encountered: