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

LoadOrCompute error handling #150

Open
mattjohnsonpint opened this issue Dec 6, 2024 · 1 comment
Open

LoadOrCompute error handling #150

mattjohnsonpint opened this issue Dec 6, 2024 · 1 comment
Labels
question Further information is requested

Comments

@mattjohnsonpint
Copy link

#103 describes how to raise an error if Compute fails. In that example, return 0, true ensures that the 0 is not added to the map.

That works fine for Compute, but I don't see a similar approach possible for LoadOrCompute - since the valueFn doesn't have a delete return value.

So, when using LoadOrCompute, how can I ensure that if an error occurs that I don't add a zero value (or nil, 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 a valueFn func() (newValue V, delete bool)?

Thanks.

@puzpuzpuz puzpuzpuz added the question Further information is requested label Dec 7, 2024
@puzpuzpuz
Copy link
Owner

puzpuzpuz commented Dec 7, 2024

Compute is flexible enough to be used in LoadOrCompute style, so you could use it instead of LoadOrCompute:

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?

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

No branches or pull requests

2 participants