On Fri, Feb 17, 2023 at 3:34 PM Jochen Voss <jochen.v...@gmail.com> wrote:
>
> I'm trying to write a generic function like the following (simplified) one:
>
> type V interface {
>     comparable // (1)
>     SomeMethod(V)
> }
>
> func Test[val V](x val) {
>     m := make(map[val]int) // (2)
>     // uses a x as key in a map and calls x.SomeMethod()
> }
>
> When I try this, see https://go.dev/play/p/cJoH5YwEHaK , I get an error 
> message for (1): "cannot use type V outside a type constraint: interface is 
> (or embeds) comparable".  If I remove the line (1) I get an error for (2) 
> instead: "invalid map key type val (missing comparable constraint)".
>
> Is there a way to make something like this work?

Are you using Go 1.20?  See https://go.dev/blog/comparable .

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/CAOyqgcXuqHXj3WLzQnVO71CgdRjyO%3DJFqvF_X%2BWD1xK-MP4heg%40mail.gmail.com.

Reply via email to