It appears to be because, deep down, the language.Tag struct contains an 
interface. While interfaces are "comparable", in that you can use == & !=, 
apparently they do not implement the  'comparable 
<https://pkg.go.dev/builtin#comparable>' constraint. 

I'm not sure why this decision was made, though I can guess. There is 
probably discussion about it somewhere. It does seem to me that this should 
be documented the  'comparable' constraint documentation 
<https://pkg.go.dev/builtin#comparable>. It also makes me wonder if there 
are other cases where '==' compiles, but the types are not 'comparable'.

On Thursday, October 6, 2022 at 7:29:46 AM UTC-4 joche...@gmail.com wrote:

> Hello,
>
> Using "golang.org/x/text/language" I have a map of 
> type map[language.Tag]int.  I would like to get the keys present in my 
> map.  Why does the following command not work?
>
>     import "golang.org/x/exp/maps"
>     ...
>     var x map[language.Tag]int
>     ...
>     fmt.Println(maps.Keys(x))
>
> The error message is "Tag does not implement comparable".  Code on the 
> playground: https://go.dev/play/p/dsyEt0ClDBH .
>
> The following function does work as expected, so this is easy to work 
> around:
>
>     func myKeys(m map[language.Tag]int) []language.Tag {
>         var res []language.Tag
>         for key := range m {
>             res = append(res, key)
>         }
>         return res
>     }
>
> But I wonder now whether it is unwise to use language.Tag for the keys in 
> a map, and why maps.Keys() requires the keys to implement "comparable" in 
> addition to the constraint "M ~map[K]V".
>
> Many thanks,
> Jochen
>
>
>
>
>

-- 
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/7f577a92-b45c-47ca-93e1-b8684a6be0fen%40googlegroups.com.

Reply via email to