-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add LoadOrTryCompute method #150
Comments
counts := xsync.NewIntegerMapOf[int, int]()
v, ok := counts.Compute(42, func(oldValue int, loaded bool) (newValue int, delete bool) {
if loaded {
return oldValue, false // return the existing value
}
// the error-aware logic goes here
return
}) Does this make sense? |
Closing this one as it's been as is for a while. Feel free to comment if something is unclear. |
Sorry for the delay. Yes, I can make |
Ok, I understand the use case now: you want to be able to skip adding a computed value. Sounds like you need a new method like |
|
#103 describes how to raise an error if
Compute
fails. In that example,return 0, true
ensures that the0
is not added to the map.That works fine for
Compute
, but I don't see a similar approach possible forLoadOrCompute
- since thevalueFn
doesn't have adelete
return value.So, when using
LoadOrCompute
, how can I ensure that if an error occurs that I don't add a zero value (ornil
, etc.) to the map? I wouldn't want a concurrent thread to retrieve that and treat it as an actual good value.TLDR: Can you please add a version of
LoadOrCompute
that accepts avalueFn func() (newValue V, delete bool)
?Thanks.
The text was updated successfully, but these errors were encountered: