On Mon, Mar 4, 2024 at 9:40 AM Jan Mercl <0xj...@gmail.com> wrote: > > The static type of an interface can be verified at compile time. The > value of a variable of an enum type (or a Pascal-like subrange) type, > cannot be verified at compile time in the general case. You would have > to add features Go does not have (like variable immutability) and/or > restrict some features for enum types that Go does have (like taking > address and using unsafe). Neither looks to me very probable, at least > in a short time. > > Those values can be however checked at runtime, similarly to > array/slice index bounds checking. For subrange-like enums it's rather > cheap and can be often eliminated. General, sparse-valued enums are > commonly too expensive to check at runtime for the compiler. to insert > them automatically.
In order to use runtime checks reliably, then for something like an enum field in a struct where we have a pointer to the struct, we would have to validate the enum value each time it is used. And presumably panic if the value is out of range. This is doable but it's hard for me to believe that this is really what people want when they say that they want enums. In particular it's not how enums work in C/C++. It is arguably how enums work in Java and Python but since they don't have pointers the situation is somewhat different. Rust enums are significantly different in that each enum value can have its own individual attached data, making them more like tagged union types. I think the biggest reason that we haven't added enums to Go, beyond the predeclared constant iota, is that there is no consensus as to what people actually want. I'm not saying that if there was consensus that we would add enum types. But I do think it's very unlikely that we would add enum types in the absence of consensus. Ian -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAOyqgcVxUFges7bUGuK-nexBu%2BzmtZ%3DHeDMFTtDYe2pauHajxg%40mail.gmail.com.