-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Closed
Milestone
Description
I'm trying to implement my own map-like data structure. I'm using generics where I have a comparable type on hand, but I don't have a way to hash it. To workaround it, I'm adding a Hash() uint64 constraint, but that requires every key type to have a custom Hash method, which is quite unfortunate, given that the Go runtime knows how to hash comparable types.
I propose we add:
// Comparable returns the hash of comparable value v with the given seed
// such that Comparable(s, v1) == Comparable(s, v2) if v1 == v2.
// If v contains a floating-point NaN, then the hash is non-deterministically random.
func Comparable[T comparable](seed Seed, v T) uint64Under the hood, this function is a thin wrapper over mapType.hasher.
There are some caveats with the operation of this function, but they're no different than the caveats of using ==.
komuw, mpx, Merovius, andy-wm-arthur, ncruces and 73 moreandy-wm-arthur, ajeetdsouza, guzmonne, hdm, mpx and 3 more