3 ms·
Now every range over your set is likely to be a bug. `map[T]bool` is simply a bad design to use.
by yencabulator 5y ago
Now every range over your set is likely to be a bug. `map[T]bool` is simply a bad design to use.
- randomswede 5y agoHow? Why? If you use the map[T]bool, you obviously need to pay attention to the bool value, even in a range. Whereas in map[T]struct{} you don't need to pay attention to the values. In either way, even with that type of data type underlying a set, I would (probably) provide a functional API to interact with it.
- yencabulator 5y agom["x"] = foo() for k := range { // if foo returned false, this is buggy }
- randomswede 5y agoYes, as I said, if you use a map[T]bool, you need to use something like: for k, v := range m { if v { } } But it also allows you to use "if m[key] { ... }". It is literally a trade-off for what convenience you want. And in both cases, you should (probably) wrap an abstraction around the raw map.