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

Map should have a method to retrieve a value but throws if the value is not present. #6640

Open
alan-knight opened this issue Nov 9, 2012 · 5 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. core-2 library-collection 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

@alan-knight
Copy link
Contributor

The current implementation of putIfAbsent in ImmutableMap is

  V putIfAbsent(K key, V ifAbsent()) {
    throw new UnsupportedError("Cannot set value in unmodifiable Map");
  }

I'd prefer it if it actually called the ifAbsent function before that. My use case is that I have a method
  externalObjectNamed(key) => externalObjects.putIfAbsent(key, () =>
      throw 'Cannot find named object to link to: $key');

which is conveniently easy to write compared to doing an indexing operation and checking the result for null. But it cannot be used if the map is immutable.

@lrhn
Copy link
Member

lrhn commented Nov 10, 2012

I don't think we'd want this.
It's clever use of putIfAbsent, but not its intended use.
If you use a put operation on an unmodifiable collection or map, you are doing something wrong. In a less dynamic language it would be a type error, and we want to fail early on this.


cc @floitschG.

@floitschG
Copy link
Contributor

I agree with Lasse, invoking a mutating method on an immutable map should throw as early as possible.

What you actually want is a method that retrieves a value, but throws if the value is not there. This is not something I'm immediately opposed to. Changing the summary accordingly.


Changed the title to: "Map should have a method to retrieve a value but throws if the value is not present.".

@DartBot
Copy link

DartBot commented Nov 12, 2012

This comment was originally written by @alan-knight


Yes, some kind of general ifAbsent method would be better in this case. Rather than having one that throws if the value isn't there, it would be nice if, like putIfAbsent, it could evaluate an arbitrary function. That could be used to throw if the value wasn't there, but also to return a default value if the value wasn't there, or other variations.

@dgrove
Copy link
Contributor

dgrove commented Jan 11, 2013

Removed Library-Collections label.
Added Library-Collection label.

@lrhn
Copy link
Member

lrhn commented Aug 26, 2013

The analogue to putIfAbsent would be "getOrElse" (really bad name, but I like the vaguely menacing sound of it :).

  V getOrElse(K key, V fallback()) {
    .. do internal lookup ..;
    if (...not there...) return fallback();
    return ... value of lookup ...;
  }
   


Removed Type-Defect label.
Added Type-Enhancement label.

@alan-knight alan-knight added Type-Enhancement area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-collection labels Aug 26, 2013
@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 triaged labels Feb 29, 2016
@lrhn lrhn added the core-m label Aug 11, 2017
@floitschG floitschG added core-2 and removed core-m labels Aug 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. core-2 library-collection 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

6 participants