Skip to content

Commit

Permalink
make generic SyncMap with with actually any type (#116)
Browse files Browse the repository at this point in the history
The SyncMap genreic type does not work with non-nilable types, as the nil value returned by sync.Map cannot be converted to the V generic type. Replace nil by the zero value if that's the case.
  • Loading branch information
francoismichel authored Jan 29, 2024
1 parent c18248f commit 14fe621
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,11 @@ func NewSyncMap[K comparable, V any]() SyncMap[K, V] {

func (m *SyncMap[K, V]) Get(key K) (V, bool) {
val, ok := m.inner.Load(key)
if val == nil {
// we can't return nil for *any* type, so we create a zero value for the type and return it, instead of nil
var zero V
return zero, ok
}
return val.(V), ok
}

Expand Down

0 comments on commit 14fe621

Please sign in to comment.