4 ms·
> Here's a trick that I expected to work but doesn't. Would be interested to hear the rationale if anyone knows it. Per the spec[0]: The comparison operat
by barsonme 11y ago
> Here's a trick that I expected to work but doesn't. Would be interested to hear the rationale if anyone knows it.
Per the spec[0]:
The comparison operators == and != must be fully defined for operands
of the key type; thus the key type must not be a function, map, or
slice. If the key type is an interface type, these comparison
operators must be defined for the dynamic key values; failure will
cause a run-time panic.
[0] - https://golang.org/ref/spec#Map_types https://golang.org/ref/spec#Map_types
- ericbb 11y agoBy "rationale" I was thinking more along the lines of "here is the reason we chose to do it this way" not just "here is the way we chose to do it".
- kjksf 11y agohttps://golang.org/doc/faq#map_keys https://golang.org/doc/faq#map_keys "Map lookup requires an equality operator, which slices do not implement. They don't implement equality because equality is not well defined on such types; there are multiple considerations involving shallow vs. deep comparison, pointer vs. value comparison, how to deal with recursive types, and so on. We may revisit this issue—and implementing equality for slices will not invalidate any existing programs—but without a clear idea of what equality of slices should mean, it was simpler to leave it out for now. In Go 1, unlike prior releases, equality is defined for structs and arrays, so such types can be used as map keys. Slices still do not have a definition of equality, though."
- ericbb 11y agoAh, perfect. Thank you.