by bravogamma · 4y ago · 0 comments
It doesn't. The closest it has is TFA's second example:
type FlagID int
const (
FooBar FlagID = iota
FizzBuzz
)
which is the underlying behaviour of C enums, but not actually that. Crucially it does not at any point hint or imply the set could be in any way closed.That it doesn't have C-style enums is a point in its favour, I would say. Not much of a point, mind, but still...
It does. By definition, an enum is simply a set of named values, which you code example provides, and is behaviourally the same as C.
> it does not at any point hint or imply the set could be in any way closed.
While true, that is a feature of sum types, which I already indicated Go does not have. This is slightly different to enums.
No.
> By definition, an enum is simply a set of named values
That is not the definition of "an enum", no. "an enum" does not imply a complete absence of any sort of type safety.
"a C-style enum" might, but that is not the distinguishing characteristic of C-style enums, the lying is, otherwise literally every language which has constants has C-style enums, including every language with sum types.
> and is behaviourally the same as C.
Except for all the ways in which C enums mislead users into assuming any sort of non-existent type-safety guarantees.
There is a critical distinction between "C-style enum", aka a misleading pile of garbage, and "just a bunch of constants". The latter is what Go provides.
> While true, that is a feature of sum types
That is not correct. For instance Java enums are not sum types, but are a type-safe, closed, set of values.
In Go the values are not in a single declaration, and not even in a single TU. The strongest hint that this is an enum and not some other kind of integral newtype is the use of `iota` on the first declaration, but `iota` is also used for other purposes.
You're technically correct that they're "behaviorally the same", but declaration structures matter a lot.