Hello,

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?

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/71d60f95-73e4-40cc-833d-dda84ef36708n%40googlegroups.com.

Reply via email to