Skip to content
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

Closed
DartBot opened this issue Apr 18, 2012 · 13 comments
Closed

Support default values when retrieving parameters from a Map #2643

DartBot opened this issue Apr 18, 2012 · 13 comments
Labels
area-library closed-obsolete Closed as the reported issue is no longer relevant core-2 P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Apr 18, 2012

This issue was originally filed by @MarkBennett


What steps will reproduce the problem?

  1. Attempt to retrieve a key not contained in a Map (like a query parameter)
  2. Receive a null value.

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.

@madsager
Copy link
Contributor

Added Area-Library, Triaged labels.

@alan-knight
Copy link
Contributor

I think this is looking for an equivalent to putIfAbsent that is just retrieveIfAbsent (but with a better name). That seems useful.

@rakudrama
Copy link
Member

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.
For literal maps there would need to be some syntax.
 {'hi': () {print('Hi');},
  'bye': () {print('Bye');},
  default: () {print('Huh?');}}
 command;
   

@rakudrama
Copy link
Member

cc @floitschG.

@floitschG
Copy link
Contributor

Removed Type-Defect label.
Added Type-Enhancement, Accepted labels.

@DartBot
Copy link
Author

DartBot commented Jul 29, 2014

This comment was originally written by tyoverby@google.com


An alternative would be to have a method on Map<K, V> called
withDefault(V defaultValue) that returns a new Map<K, V>
which is simply a view on the receiving map that returns
defaultValue when asked for a value.

@eseidel
Copy link
Contributor

eseidel commented Aug 22, 2015

Any of these solutions would be fine. Right now I have to write the following:
Type value = map.containsKey(key) ? map[key] : default;
which is very verbose compared to python's:
value = map.get(key, default)
I commonly want this sort of implied-defaults behavior when reading config files, etc.

@Hixie
Copy link
Contributor

Hixie commented Aug 23, 2015

With null-aware operators, assuming "null" isn't a valid value, you can just do:

Type value = map[key] ?? default;

@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug and removed accepted labels Feb 29, 2016
@sircco
Copy link

sircco commented May 22, 2017

+1 this would come handy for us too

This was referenced Aug 9, 2017
@lrhn lrhn added the core-m label Aug 11, 2017
@floitschG floitschG added core-2 and removed core-m labels Aug 31, 2017
@matanlurey matanlurey added the closed-obsolete Closed as the reported issue is no longer relevant label Jun 19, 2018
@sassman
Copy link

sassman commented Nov 26, 2018

so why this got closed? @matanlurey

@lrhn
Copy link
Member

lrhn commented Nov 26, 2018

It would be a breaking change of the Map API to add it now.

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 ?? defaultValue approach, or create a Map wrapper that returns a different value for non-existing keys, and not change the Map API at this point.

@sassman
Copy link

sassman commented Nov 26, 2018

sure, workarounds are there enough. But it would rather be consistent to add since putIfAbsent is there. Also worth considering that python and other languages have that as default functionality.

Do you accept contributions regarding such changes @lrhn ?

@icatalud
Copy link

I would add a named parameter {Function() defaultValueGenerator}. But yes, it doesn't sound like a trivial change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-library closed-obsolete Closed as the reported issue is no longer relevant core-2 P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests