top | item 46335590

(no title)

cabirum | 2 months 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.

discuss

order

tym0|2 months ago

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

Joker_vD|2 months ago

I could've sworn we got "sets" in the Go's standard library along with the "maps" module but... apparently not? Huh.

ioanaci|2 months ago

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.

rplnt|2 months ago

Isn't it empty interface that's idiomatic? Or was anyway?

edit: I may be wrong here