top | item 18318703

Proposal: Go 2 enum type

4 points| srfrog | 7 years ago |github.com

1 comment

order

breakingcups|7 years ago

The only thing I want from enums in Go is to be able to specify a list of valid states for a certain datatype, optionally specifying the underlying integer value.

Go is currently lacking this because:

* Regular const expressions with iota aren't enough as I can assign those to other data types accidentally and the compiler won't complain

* Const expressions with an explicit type and iota aren't enough as I have to specify the type every possible value, rather than just grouping them all under one "type umbrella" so to speak.

* All const expressions (typed or untyped) can be scattered throughout the program in an inconsistent manner

This proposal makes no sense to me whatsoever though. It's definitely not what I would describe as an enum. The other proposal it links to however makes a lot more sense. It's a shame a lot of Go developers seem to come at this issue from a C enum perspective which brings a lot of baggage while there are enums in other languages that are a lot better examples of what enums should be in my opinion.