I have a slice of reflect.Type
values that define the input type for a function and I want to cache the result using sync.Map
.
I realize slices cannot be used as map keys in general so I have searched far and wide for a way to create unique keys from multiple reflect.Type
values. Ideas include
- Using a concatenation of
reflect.Type.String()
for each subkey. The godoc explicitly says not to use it for equality so the keys would not be unique. - Use a concatenation of
fmt.Sprintf("%#v", subKey)
- see here. I don't know whether these keys would be unique. - Create a tree out of
sync.Map
s [a map of maps of maps... of depth len(slice)] where the lookup for a value involves traversing deeper in the tree of maps for each subkey. This seems slow and cumbersome but should work. - Hardcode a limit on the number of keys and store it as an array whose size is the upper limit. This would work but the hardcoded limit is annoying when you need to exceed the limit.
A reference on why slices can't be used as keys indicates that how to define equality on slices may be unclear. In this case I want element-wise equality if and only if the slices have the same length.
It would be awesome to find a byte representation of reflect.Type
similar to the solution to a similar problem here.
See also hashing-reflect-type.