On Thu, Dec 23, 2021 at 12:42 PM aind...@gmail.com <aindu...@gmail.com> wrote: > > Now that generics is coming along, is there an easy way to extend the builtin > map type to use a non-comparable key type? For example, given this constraint > > type Key[T any] interface { > Size() uint64 > Hash(seed uint64) uint64 > Equal(T) bool > } > > and a type Foo like > > type Foo []int > func (a Foo) Size() uint64 { ... } > func (a Foo) Hash(seed uint64) uint64 { ... } > func (a Foo) Equal(b Foo) bool { ... } > > I'd like a map[Foo]struct{}. Is this possible now? Or does the runtime map > have to be updated to support this?
The addition of generics is independent of whether the builtin map type can support non-comparable key types. With generics it now becomes possible to write a new map type with explicitly specified hash and equality functions. This could work much like the builtin map type, but it would have methods (e.g., Lookup, Insert) rather than special syntax. However, nobody has written such a type yet as far as I know. Note that if anybody wants to write such a type, I recommend passing in comparison and equality functions rather than requiring the key type to have methods, as I mentioned in the talk at https://www.youtube.com/watch?v=Pa_e9EeCdy8 . 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/CAOyqgcV_JTYoETPMsew_ZEsXnze%3Dq4roVvH4wbtZVHbB4xoOQQ%40mail.gmail.com.