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
It is possible to have a type parameter representing any type usable for the key of a map
What did you see instead?
It is not possible to have a type parameter representing any type usable for the key of a map. Specifically the key type any cannot be used in a type parameter, whereas it is possible to use directly as a key with map[].
For instance:
type MapBool[K any] struct {
m map[K]bool
}
Fails because K has a missing comparable constraint.
However, if we use
type MapBool[K comparable] struct {
m map[K]bool
}
then
type m3 = MapBool[any]
fails because any does not implement comparable
However
type m1 = map[any]bool
works just fine.
This makes it difficult (impossible?) to implement a generic with the same flexibility as map (FWIW I was trying to make a generic based sync.map). It seems that map requiring a comparable might be overconstrained as map[] appears to be able to successfully compare interface{} (i.e. any)
What version of Go are you using (go version)?
$ go version
go1.18rc1
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (go env)?
(go 1.18 - details at the bottom)
affected/package: base
What did you do?
Attempt to make a generic map. A simple example is at https://go.dev/play/p/odFEAA0i5uZ?v=gotip
What did you expect to see?
It is possible to have a type parameter representing any type usable for the key of a map
What did you see instead?
It is not possible to have a type parameter representing any type usable for the key of a map. Specifically the key type
any
cannot be used in a type parameter, whereas it is possible to use directly as a key withmap[]
.For instance:
Fails because
K
has a missing comparable constraint.However, if we use
then
fails because
any
does not implementcomparable
However
works just fine.
This makes it difficult (impossible?) to implement a generic with the same flexibility as
map
(FWIW I was trying to make a generic basedsync.map
). It seems thatmap
requiring acomparable
might be overconstrained asmap[]
appears to be able to successfully compareinterface{}
(i.e.any
)What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputThe text was updated successfully, but these errors were encountered: