News for hackers. Stuff that matters.
Re:Go is boring, and that’s fantastic by H1Supreme on Thursday July 02, 2020 @10:01AM (#23713163)
I write Go nearly everyday, and enums are the #1 thing I'd like to see. Honestly, it's kind of surprising that the language lacks this feature.
Re:Go is boring, and that’s fantastic by tleb_ on Thursday July 02, 2020 @10:46AM (#23713755)
See here for an explanation. Not sure it's up to date though. https://golang.org/doc/faq#variant_types
Re:Go is boring, and that’s fantastic by tsimionescu on Thursday July 02, 2020 @03:01PM (#23716736)
Variant types are very different from enums. Variant types allow you to define a type that is "one of these N types". Enums are the same thing at the value level: they allow you to define a type that is "one of these values". You can't implement one in terms of the other.

Variant types do have some similar use cases with interfaces, but I don't see any relationship with enums in terms of use cases, so I don't think that applies.

Also, let's not forget that even C has enums, so their lack of inclusion in Go is baffling (especially since they went and implemented the much more arcane yet limited `iota` feature).

Re:Go is boring, and that’s fantastic by masklinn on Thursday July 02, 2020 @03:16PM (#23716903)
> Also, let's not forget that even C has enums

Not in any useful sense of the word. A C enum is a typedef and a few named constant, it's absolute shit, and if the choice was restricted to "C enum or nothing" then "nothing" was absolutely the right call.

Go's typedecl + iota is actually a step up from C's enums, because (aside from "untyped constants") you need an explicit conversion from "any random integer" to your typedecl. It's not any more reliable or any safer at point of use, but it will prevent some misuses, or at least force the developer to consider what they're doing.

Re:Go is boring, and that’s fantastic by cellularmitosis on Thursday July 02, 2020 @08:34PM (#23719728)
Yeah, I think the confusion stems from the word "enum" being used to mean different things in different langs.

A great illustration lies in comparing enums in Objective-C (essentially just named integers), whereas the associated values in Swift enums make them "Sum" types.