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

Horrible isn't it.

Not sure why they don't just use a struct with some fields, then just create instances of those instead of magic comments and a preprocessor.

https://go.dev/play/p/uUNHxM8zqY9

Mercury = {"Mercury",190.0,...}

It would not be any more verbose and would let them define constants for use elsewhere if they wish. If they want to control values for validity they can use a NewPlanet func.

I do agree go's enums suck but they need to take a step back and look at what they've created and what would be an easier way to create this if they really need it. It seems like they want real types here, not an enum and if they do want constant planet types it's very easy to make them without all this busywork.




> I do agree go's enums suck

There's not much more you can do with them. A enumeration is literally assigning a number to something. That is all. Enums suck, fundamentally.

Other language features, like value constraints, can help improve the experience of using enums, but, really, if we are inventing a language, why have enums at all? If you think you have a use case for enums, a more advanced type system will offer a better model for your problem.

Sure, enums were a clever hack in the days of yore when we didn't have a good grasp of type theory, but are pointless in a modern language. Of course, Go is purposefully trying to be a language of yore, so it stands to reason that it would have them and all the suckiness that goes along with it, but, again, if we want it to be a modern language then you don't need to hack on top of enums with new features to make them nicer. You can get rid of them entirely.


They clearly want an enum/sum type which an array of structs (which is the trivial solution they started at) doesn't provide.

You're seeing someone try to address their issues with X and suggesting "here's how you use X." It doesn't track.


Nope, I'm saying don't use X at all, because enums are not the right thing to use to solve this problem. This solution of magic comments and a precompiler is worse than not using enums at all.




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

Search: