Repository navigation
entry API v3 - #921
entry API v3#921
Conversation
|
How about map.entry(key).or_insert(0) += 1
let val = map.entry(key).or_insert_with(|| expensive(big, data));
map_of_vecs.entry(key).or_insert_default().push(0);Drawback: |
|
It seems to me that we could get many of these benefits by replacing |
|
@reem that does something different (just returns the default value, ignoring the error, not inserting the default value into the data structure). |
There was a problem hiding this comment.
- Pass more information into the
default_withclosure.
There was a problem hiding this comment.
There's literally no information to pass for a vacantentry.
There was a problem hiding this comment.
@gankro Was this addressed somewhere off-thread or there's a technical reason it can't be done?
It would be neat to save a clone if the 'expensive' case requires the key to generate the value data.
There was a problem hiding this comment.
This API as a thin layer over Entry can't admit an &Key for the simple reason that Entry doesn't expose this. Otherwise it wasn't included in Entry simply because no one presented a compelling usecase (iirc every use of the old API I saw never used the key even though it was given). It's possible to add new methods that expose this functionality at a later date, though.
|
@huonw You're right. +1 to this RFC then, I think this is quite clean. |
|
I concur with @Stebalien, I find the |
|
Just as an interesting note, Python has something similar called |
There was a problem hiding this comment.
you accidentally the tail end of this attr
|
This is nice, and hits all my uses of |
|
In the Java world, there are some libraries aiming to enhance collections usage, which have wildly different APIs, e.g. trove has adjustOrPutValue, while koloboke and Java 8 have compute, computeIfPresent, computeIfAbsent and getOrDefault. Python has a In this context, the suggested API feels right. It's certainly more orthogonal than any of the API designs of other languages. 👍 |
|
Really like the RFC. Bike-shedding names: here's another suggestion (not necessarily better):
|
|
I like the idea but I am not wild about the name. Certainly before reading the RFC I had no idea what |
|
That would be a great addition, as others already voiced I'm unsure about the naming. |
|
Well done, @gankro! I agree with what many have said about the naming (especially overlap with the |
|
👍 for or_insert as livable. |
|
👍 can't get clearer than |
There was a problem hiding this comment.
self for sure. Want to consume the entry.
|
Very nice API, I'd prefer the name |
|
On Tue, 3 Mar 2015 at 10:35 bluss [email protected] wrote:
|
|
This RFC has been merged with overwhelming consensus. The merge updates the text to the |
Replace Entry::get with Entry::default and Entry::default_with for better ergonomics and clearer code.
Rendered