-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Feature request: Intermediate API for updating collections, especially AbstractDict
#31199
Comments
To get the ball rolling, an example how this could possibly look like:
Most of the timing discrepancy between The above intermediate API is too ugly to use for end-users, but not too hard for package authors. Obvious simplifications: Skip the "invalidate iterators" rigamarole, skip the "skip updates? delete entries?" rigmarole. The reason I am interested in The reason I am interested in iterator invalidation is that I need to iterate over key-value pairs, and possibly update the value (or delete it alltogether), without touching any except for the current one. |
I would call this |
In order to facilitate That is almost good enough to be user facing ( Is there a non-ugly way of including a result-flag in the Cramming a |
Simply on bikeshedding the name, I feel we should probabably provide seperate functions for insert, update and “upsert” (current Just something to consider if ever we were to want an |
In #31362 I put forth the idea of using a 0-Dimension array, make something very similar work. Right now the best way I had to make an
|
xref older discussion #24454 |
Can't we just export a 4-argument get!(f::Function, collection, key, default) =
collection[key] = f(get(collection, key, default)) ?? i.e. like This would not be breaking (it would not export any new symbols), would be easily discoverable as another method of I'm tired of not having an API for this in Base, and adding another |
I think that makes sense. This is similar (modulo name) to #33758 |
One of the most common operations on
AbstractDict
is updating values in-place, like e.g.d[k] += v
orhaskey(d, k) ? d[k]+= v : d[k] = v
.It is unlikely that user-code with that idiom will ever get optimized to perform only a single look-up, and previous discussions got stalled on what is the preferred user-facing API.
I propose to introduce a new un-exported but stable and documented intermediate API. Design goal is maximal flexibility, at the cost of arbitrary uglyness. Then, package authors who implement an
AbstractDict
can specialize this intermediate API to accelerate updates, and package authors who useAbstractDict
can implement their favorite beautiful user-facing API that uses the intermediate API.edit: Excised preliminary proposal to separate comment. If you vote on the issue, use this comment for expressing feelings about the general idea of an intermediate ugly API and the below comment for expressing feelings about the specific preliminary proposal.
The text was updated successfully, but these errors were encountered: