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