-
Notifications
You must be signed in to change notification settings - Fork 213
allow to destructure record types with positional fields #3782
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
This are more fitting for the Dart Language issue tracker. Also, your last suggestion are very similar to: #3001 |
Wasn't sure if that is a language feature or a compiler issue. Please move/triage as you see fit. |
final (int r, int g, int b) color = (1, 2, 3); Since you have put the variable name Are you asking for this syntax instead should define the variable final (int r, int g, int b) = (1, 2, 3); Here, we don't have |
I know. The catch is that compiler allows these param names when record name is declared after its type declaration. And that declaration looks similar to what you get when declaring record type in parameters of other methods. In my other example, the type of |
The syntaxes for types and for patterns are eerily similar, for very good reasons, which is actually why what you're trying doesn't work So you would have to write final (int r, int g, int b) & color = ...; to bind all the names ... if declaration patterns allowed |
We can actually use a variant, as long as we stick to an void main() {
final ((int r, int g, int b) && color) = (2, 3, 4);
...
} |
I wonder if parser could delay its decision about exact type or infer a pattern for the time being until it is time to disambiguate exact type? I used variable declaration for record, just to illustrate that compiler takes it as a valid syntax. Don't really have use for that variable (unless Dart would allow to do something like Other syntax variants discussed here and in dart-lang/language#3001 are adding some syntactic noise. Out of all, this one |
See also #3487. |
So this is a request for two things:
This is a language issue, so moving to language repository. |
The Dart language allows to define a positional fields record and then destructure its fields like so:
Also, in the following snippet the record type declaration containing field names is valid, but the field names declared in the record type can't be dereferenced:
It would be great if the Dart language allowed to dereference these values by name. This would be a really handy feature when records with positional fields are passed around.
Consider the following example. The record type declaration in the
map()
method is valid, but these parameters can't be used in the lambda expression.Currently we have instead write lambda like
(en) => '${en.$1}:${en.$2}'
, which is much less readable code.The text was updated successfully, but these errors were encountered: