3 ms·
> isn't generic Sets easily implemented with map being already generic? Since Go has neither generic functions nor generic typedefs you can't implement a Set w
by masklinn 6y ago
> isn't generic Sets easily implemented with map being already generic?
Since Go has neither generic functions nor generic typedefs you can't implement a Set with a generic key type on top of map, you have to reimplement all the set operations for each key type you use.
- jjtheblunt 6y agoI see what you mean there.
- coddle-hark 6y agoI think map[T]bool is already a pretty good set; the only things you can do with sets are insertion, deletion, iteration and checking for existence and they're all well-supported. Of course, if you need a concurrent set you're right back in type system hell.
- erik_seaberg 6y agoYou can’t write intersection, union, difference, subset (contains all), or powerset as reusable functions for any element type. The idiomatic thing for now is to rewrite them as loops over and over, but that’s error prone, hard to read, and not a good use of time.
- EdwardDiego 6y agoThis was the exact issue I hit. There are set libraries for Go, but they only handle standard types. The for loops got old quick.
- simiones 6y agoIn addition to that, map[T]bool only works if T is one of the few types that Go can check for equality automatically. You can't define a custom equality (+hash) function for your type and use it with the built-in map.