4 ms·
Use a slice of strings? type Color int const ( Red Color = iota Green Blue ) var Colors = []string{ "Red", "Green", "B
by akira2501 2y ago
Use a slice of strings?
type Color int
const (
Red Color = iota
Green
Blue
)
var Colors = []string{ "Red", "Green", "Blue" }
Now (Colors[Red] == "Red") and (slices.Index(Colors, "Green") == Green).
- slekker 2y agoYou could do the same with a map[Color]struct{} and the lookup would be "if _, ok := Colors[Green]; ok {"
- JyB 2y agoCompile-time safety is not achieved which is the point.
- akira2501 2y agoIt does not exist in the implementation. We can discuss workarounds which make it more convenient and directly address one of the points in the article, or we can discuss something which is definitely not going to change in the lifetime of the language. There is no language that completely isolates you from runtime hazards.
- rty32 2y agoIf you add an enum entry but forget to update the slice, BOOM And that's exactly the kind of thing people are discussing here.