Hacker News new | ask | show | jobs
by cabirum 176 days 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.

3 comments

Using 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`.

I could've sworn we got "sets" in the Go's standard library along with the "maps" module but... apparently not? Huh.
Almost 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

Honestly insane in 2025 to not have a generic Set.
I 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.
Isn't it empty interface that's idiomatic? Or was anyway?

edit: I may be wrong here