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
Support default values when retrieving parameters from a Map #2643
Comments
Added Area-Library, Triaged labels. |
I think this is looking for an equivalent to putIfAbsent that is just retrieveIfAbsent (but with a better name). That seems useful. |
Unlike putIfAbsent, this would work on a const Map. Python dict uses 'get'. Dart could do the same. class Map<K, V> .... { V operator[](K index) => this.get(index, null); V get(K index, V valueIfAbsent) { ... } An alternative is to parameterize the map instance with a default value. |
cc @floitschG. |
Removed Type-Defect label. |
This comment was originally written by tyoverby@google.com An alternative would be to have a method on Map<K, V> called |
Any of these solutions would be fine. Right now I have to write the following: |
With null-aware operators, assuming "null" isn't a valid value, you can just do: Type value = map[key] ?? default; |
+1 this would come handy for us too |
so why this got closed? @matanlurey |
It would be a breaking change of the It was a change we considered for Dart 2.0, but did not have sufficient priority, and there were available workarounds. I would either use the |
sure, workarounds are there enough. But it would rather be consistent to add since Do you accept contributions regarding such changes @lrhn ? |
I would add a named parameter {Function() defaultValueGenerator}. But yes, it doesn't sound like a trivial change. |
This issue was originally filed by @MarkBennett
What steps will reproduce the problem?
What is the expected output? What do you see instead?
I would expect to be able to set a default value either when the value is retrieved or before hand. Using Map#putIfAbscent() is awkward for many defaults.
Alternatively, adding a merge function to the Map interface which merged together two Maps could also work well.
What version of the product are you using? On what operating system?
Version 0.1.0.201204121423, Build 6479
Dart SDK version 6478, Dartium version
Please provide any additional information below.
In general, adding more of these basic Map and List operations will make them much more usable and the Dart more productive in general.
The text was updated successfully, but these errors were encountered: