8 ms·
> Using empty structs also hurts readability An empty struct is idiomatic and expected to be used in a Set type. When/if the memory optimization is reintroduce
by cabirum 9mo ago
> Using empty structs also hurts readability
An empty struct is idiomatic and expected to be used in a Set type. When/if the memory optimization is reintroduced, no code change will be needed to take advantage of it.
- ioanaci 9mo agoI also feel like map[T]struct{} communicates its purpose way better than map[T]bool. When I see a bool I expect it to represent a bit of information, I don't see why using it as a placeholder for "nothing" would be more readable than a type that can literally store nothing.
- tym0 9mo agoUsing a bool instead of empty struct also means that there is more way to use it wrong: check the bool instead of if the key exist, set the bool incorrectly, etc... I would argue using bool hurts readability more. Even better write/use a simple library that calls things that are sets `Set`.
- Joker_vD 9mo agoI could've sworn we got "sets" in the Go's standard library along with the "maps" module but... apparently not? Huh.
- kevindamm 9mo agoAlmost made it into 1.18 but looks like it doesn't add enough value and has some open questions like what to use for a backing data type and what complexity promises to make. https://github.com/golang/go/discussions/47331 https://github.com/golang/go/discussions/47331
- vips7L 9mo agoHonestly insane in 2025 to not have a generic Set.
- frou_dh 9mo agoe.g. https://pkg.go.dev/github.com/zyedidia/generic/mapset https://pkg.go.dev/github.com/zyedidia/generic/mapset
- rplnt 9mo agoIsn't it empty interface that's idiomatic? Or was anyway? edit: I may be wrong here