Live data from Hacker News

Enums in Go

dizzy.zone

51–57 of 57 posts

Re: Enums in Go

#51
Besides the iota dance hack of "enums" in Go, apparently Go 1.23 is going to bring another one, magic fields.

https://pkg.go.dev/structs@master

What more sane languages would use attributes for, Go 1.23 will do it like this,

    type myStruct struct {
        _  struct.HostLayout 
    }
Lovely design.

Re: Enums in Go

#52
post #35

Earlier quoted context omitted.

> I am not a huge fan of go:generate and similar projects. They add a level of unknown that goes against the core Golang design values. I'm not sure I would agree with that. go:generate is a core part of Go since v1.4 and the enum generators are the kind of thing that was intended. https://go.dev/blog/generate That said, enums would be a welcome improvement to the language. But even then, I think go:generate has a pl…

"add(ing) a level of unknown" is a very fair description from a code comprehensibility standpoint, since the go:generate directive is about running arbitrary executables.

The is ameliorated somewhat by the fact that go generate is not part of the build process. Its output would typically be checked into the repo. So as long as the generated code is reasonably readable, it should not have too much of an impact on code comprehensibility.

Re: Enums in Go

#53
post #50
post #26

Earlier quoted context omitted.

Only if you remember, which you, or someone else, is bound to not eventually

But you're not adding variants without a reason? You want them to have some effect. It's hard for me to think of an example where it would make even sense to "having to remember to handle the variant" rather than "handling the desired effect of the variant".

People are stressed, get distracted, are tired, don't have complete knowledge etc. This is kind of like arguing that null pointers aren't a problem, you "just" have to check all usage of the pointer if you make it null. In practice we know solutions like this don't work

Re: Enums in Go

#54
post #23
post #18

The problem isn't so much the lack of enums, it's more that there is no way to do exhaustive pattern matching at compile time. I have seen production systems go down because someone added a variant to an 'enum' but failed to handle that new variant everywhere.

There are two linters that add those checks: https://github.com/nishanths/exhaustive for values and https://github.com/alecthomas/go-check-sumtype for types. Both are integrated into golangci-lint. I use both a lot and have only a positive experience. Having support from the language would be nice, though.

These both look like great tools. I'll give them a spin!

Re: Enums in Go

#55
post #22
post #18

The problem isn't so much the lack of enums, it's more that there is no way to do exhaustive pattern matching at compile time. I have seen production systems go down because someone added a variant to an 'enum' but failed to handle that new variant everywhere.

Isn’t that the first thing you would do if you add a new variant?

Huge codebase. It was handled in 13 places, but they missed the 14th.

Re: Enums in Go

#56
post #16

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).

Compile-time safety is not achieved which is the point.

It 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.

Re: Enums in Go

#57
post #48
post #27

Enums in Go are not good. Code generation just makes it worse. Both because code generation has a bad smell and because nobody can agree on how to do enums in Go so we just end up with lots of diverging solutions. Go 2 needs to have more usable enums. And while I'm not a big fan of "adding more stuff" to languages, it wouldn't hurt Go to learn a couple of things from Rust.

I can't see why they wouldn't be in a future 1.x release

I guess it depends on how much of a departure from the current language it would be and if that introduces complicating factors. It would make sense to seriously consider adding matching (like in Rust) but then you might want to consider more than just enums?

I'm not a language design expert, but I suspect there be worms in that there can.

Post reply on HN