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

Method name bikeshedding #60

Open
dminor opened this issue Oct 24, 2024 · 10 comments
Open

Method name bikeshedding #60

dminor opened this issue Oct 24, 2024 · 10 comments

Comments

@dminor
Copy link
Collaborator

dminor commented Oct 24, 2024

The proposal would add two new methods to Map:

  • getOrInsert(key, default) - Return value corresponding to the key if present, otherwise insert the specified default value and return that.
  • getOrInsertComputed(key, callback) - Return value corresponding to the key if present, otherwise call the callback function to determine the default value to insert and return.

Those names are placeholders, I'm opening this issue so we can bikeshed the final names to be used.

@dminor
Copy link
Collaborator Author

dminor commented Oct 24, 2024

There was a previous conversation (see #29) about naming for the original design of this proposal.

@dminor dminor mentioned this issue Oct 24, 2024
@nex3
Copy link

nex3 commented Oct 25, 2024

In Dart this is called putIfAbsent(), although setIfAbsent() might be a better fit given Map.set().

@dminor
Copy link
Collaborator Author

dminor commented Oct 31, 2024

cc a few people whom I recall expressing an opinion on the naming at plenary: @ljharb @rkirsling @acutmore @rbuckton @bakkot @syg

@bakkot
Copy link

bakkot commented Oct 31, 2024

I don't like setIfAbsent because it emphasizes the action of inserting, whereas the primary use for this is fetching an existing value.

getOrInsert is good, but I don't love getOrInsertComputed. Nothing better comes to mind, though.

@zloirock
Copy link
Contributor

Both current names LGTM - definitely better than was before since they perfectly describe their logic.

@acutmore
Copy link

+1 to prefixing with get.... To add further rationale, Map.prototype.set returns the map instance, not the inserted value.

@rkirsling
Copy link
Member

I might be inclined to suggest getWithDefault, since the one issue with getOrInsert is that we want to get either way, we just might need to insert first.

I suppose this would also allow us to rearrange ...Computed into getWithComputedDefault.

@acutmore
Copy link

acutmore commented Nov 1, 2024

I suspect some may interpret getWithDefault as a method that never mutates the map - the method form of map.get(key) ?? defaultV.

@bakkot
Copy link

bakkot commented Nov 1, 2024

I might be inclined to suggest getWithDefault, since the one issue with getOrInsert is that we want to get either way, we just might need to insert first.

You can look at it that way, but you can also look at as the insert case just returning the inserted value.

@ljharb
Copy link
Member

ljharb commented Nov 1, 2024

Just to throw out an extreme alternative, insertIfMissingThenGet (I’m not actually suggesting we do this one, ofc)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants