Makes sense. Thanks!

On Wed, Apr 27, 2022 at 4:47 PM Ian Lance Taylor <i...@golang.org> wrote:

> On Wed, Apr 27, 2022 at 3:56 PM will....@gmail.com
> <will.fau...@gmail.com> wrote:
> >
> > 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?
>
> Currently a for/range statement using a value whose type is a type
> parameter is only permitted if the type parameter constraint gives it
> a core type.  Basically, a core type is an optional tilde followed by
> a single type.  It is not a union.  See
> https://go.dev/ref/spec#Core_types.
>
> So the compiler is telling you that the type parameter KV2 has no core
> type, so for/range is not permitted.
>
> We impose this restriction to simplify the meaning of for/range, and
> to avoid having to figure out what it would mean to range over a type
> parameter whose constraint is []byte | map[int]string | chan float64.
>
> It is possible that we will lift this restriction in a future release.
>
> Ian
>

-- 
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 golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAKbcuKidrwo-mQfP8QBk_ExAbT%3DDChifyk5A1CzrAsBQxD9qbQ%40mail.gmail.com.

Reply via email to