Do slices and maps have compatible type sets regarding the range operator?
I was trying to iterate through either, where slice keys are the indexes,
such that this would work:
type KV[K comparable, V any] interface {
~[]V | map[K]V
}
func f[KV2 KV[K, V], K comparable, V any](kv KV2) {
for k, v := range kv { // line 12
fmt.Println(k, v)
}
}
func main() {
f[map[string]string, string, string](map[string]string{"a": "b", "c":
"d"}) // works
f[[]string, int, string]([]string{"e", "f"}) // error
}
I get this error:
./prog.go:12:20: cannot range over kv (variable of type KV2 constrained by
KV[K, V]) (KV2 has no core type)
Play link: https://go.dev/play/p/jSfaEaTmcQ1
I'm having trouble understanding this error message. Is this saying that
maps and slices don't have a core type because they're not compatible for
the range operator?
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/69a847c2-cf16-40c4-b6b9-22569009731an%40googlegroups.com.