Hacker News new | past | comments | ask | show | jobs | submit login

I'm not saying integer literals shouldn't be polymorphic. I'm just saying that polymorphism should only extend to numbers, not enums.



A desire to restrict the polymorphism of integers is fine, but it doesn't really change the type safety argument at all.

"Enums" here are just another type of integer, exactly like in C# and C++. They're not a separate concept. I wish Go had Sum Types or even just exhaustive, non-integer (as far as the programmer knows) enums, but neither of those are requirements to have a type safe enum. The only difference vs C++ and C# is that Go has untyped literals, which are quickly handed a type based on where they're used.

Enums in Go have type safety, which is the point you disagreed with. You can't assign values of the wrong type to an enum-typed value without explicit conversion in Go. That's type safety.

There are linters that can be used if you want to enforce more restrictions: https://github.com/THE108/enumlinter

Linters that fit your team's expectations are a good thing to use in any language, and Go is no exception here.

Would this linter be unnecessary in other languages? Sure. I would argue it's still unnecessary, because I've never even once seen anyone accidentally type a literal value in Go where they meant to use one of the predefined enum values. It's certainly possible, but it hasn't caused me any lost sleep.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: