-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add .records
getter to Map (as an extension method)
#54965
Comments
|
I hope not, because that's what I plan to do eventually.
Yes, please! |
About the idea itself (if we can't get rid of the type when destructuring), it's not impossible, but it's also not particularly important when you can just write |
I like the idea of having a lighter-weight pattern when the context provides the type. for (final (id, name) in namesById.entries) {
//
} How might this work? The record-pattern in destructuring is reconsidered as an object-pattern when the subject of descructuring has a type where there is a primary constructor that can fill in the missing parts of the object-pattern (in this case, the class/interface |
No constructors needed. Object patterns are not based on constructors. It would just say that a syntactic record pattern with no positional fields in a declaration pattern, where the matched value type is not a record type, and not It'll probably give the matched value expression a context type which is a record type. It just doesn't have to satisfy that context type. Would be for (var (:key, :value) in map.entries)... The alternative, discussed in dart-lang/language#4124, is to have a short marker before the parentheses: |
I would like a concise way to not having to use the names |
Constructors are unrelated to destructuring. There is not necessarily any direct connection between constructor parameters and object state, or between object state and accessors. The only part of a class that you can rely on being intentional, is its public API. That's why the only thing destructuring can access is getters, and only by name. That's all that the class actually promises. (It should be able to call other methods too, IMO, but still only explicitly declared object APIs.) If you want to destructure a map entry without writing I don't want to add such a getter to maps. Map entries are not anonymous pairs, maps are not just sets of pairs. The |
Given that changing the implementation of
MapEntry
to a record like({K key, V value})
would be a big breaking change, I'm opening this issue to see the feasibility of adding arecords
getter as an extension to the Map class. This would be similar to what was done withindexed
on Iterable. This is something I could probably open a PR for myself but I wanted to create this first to get thoughts from the Dart team and others.In my projects I have it defined as:
In the SDK, I would assume this implementation would change to returning a custom implementation of Iterable (like
IndexedIterable
forIterable.indexed
).The usage would look like this:
Of course, this would all be moot if we could just remove the type when destructuring 😉
The text was updated successfully, but these errors were encountered: