You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
func (m *Map) LoadOrStore(key, value interface{}) (actual interface{}, loaded bool)
The proposal states:
That LoadOrStore accept an optional argument that forces it to interpret value more intelligently.
"More intelligently" is defined as: if value is of type FuncLit with a single return value, then if the key does not exist in the map, it callsvalue and stores the return value.
Depending on the ambit of Go1 compatibility promise, feel free to mark this as a Go2 proposal.
Alternatively, a new method can be added to Map that has this behaviour built-in.
The text was updated successfully, but these errors were encountered:
pjebs
changed the title
proposal: sync: map LoadOrStore interpret functions for value
proposal: sync: Map LoadOrStore interpret functions for valueApr 7, 2021
@ianlancetaylor if I understand correctly the proposal is to add a special case where if value interface{} is a func() interface{} (say) then use it as a callback. Now that isn't backward compatible as-is, but if we added a new type type LoadFunc func() interface{} and only looked for values of that named type then it would be.
To me, overloading this API by shoehorning a callback into the value interface{} seems worse than adding a new method.
Either way, though, I think this has a sharp edge. The callback function cannot block any other Map method, including concurrent LoadOrStores (that would be antithetical to sync.Map). (To put it another way, it is not possible to implement that code snippet which includes a user-provided callback atomically without introducing unacceptable blocking.) It follows that the callbacks may be called multiple times concurrently. I think users may find that surprising.
The proposal states:
value
more intelligently.value
is of typeFuncLit
with a single return value, then if the key does not exist in the map, it callsvalue
and stores the return value.Depending on the ambit of Go1 compatibility promise, feel free to mark this as a Go2 proposal.
Alternatively, a new method can be added to
Map
that has this behaviour built-in.The text was updated successfully, but these errors were encountered: