3 ms·
Obviously Go code shown here doesn't show string enums. It's an integer enum with function that converts integer value to the string representation (by implemen
by divan 4y ago
Obviously Go code shown here doesn't show string enums. It's an integer enum with function that converts integer value to the string representation (by implementing Stringer interface). Which makes total sense (not every int enum needs String conversion).
I assume your Java example has some magic (aka conventions) in how to convert values into string. Without reading Java manuals, how do I know what's the magic's conversion function? With single words it's usually easy, but try two words "LeapYear" or text with non-Unicode chars - how the magic will work then? Go tries to minimize amount of hidden magic. Also, should not Java enums be capitalized?
I mean, sure, Go enums can be improved, and there are few proposals (none of them yet passed, and that's usually for a reason), but this comparison is just incorrect.
- noisy_boy 4y ago> Obviously Go code shown here doesn't show string enums. It's an integer enum with function that converts integer value to the string representation (by implementing Stringer interface). Which makes total sense (not every int enum needs String conversion). Considering your robust defence of Go, maybe you can demonstrate the simplicity of Go's string enums? > I assume your Java example has some magic (aka conventions) in how to convert values into string. Without reading Java manuals, how do I know what's the magic's conversion function? There is no magic - .toString() produces an equivalent string. The point is to keep simple things simple - most of the time, the enum value and its string representation is same and the snippet in my previous post automatically gets us there. > With single words it's usually easy, but try two words "LeapYear" or text with non-Unicode chars - how the magic will work then? Go tries to minimize amount of hidden magic. Thanks for acknowledging that it is atleast easy in Java for single words. Words with spaces or unicode (I think thats what you meant when you said non-Unicode) isn't terrible either: enum Season { SUMMER("S U M M E R "), AUTUMN("A U T M N "), WINTER("W I N T E R "), SPRING( "S P R I N G "); private String value; private Season(String value) { this.value = value; } public String toString() { return this.value; } } I had to introduce a variable and couple of one-line methods which isn't super simple but still a win over a switch statement that requires listing out every representation. For shits and giggles, this is how I can enumerate over the enum values in Java: for (Season s : Season.values()) { System.out.println(s); } Feel free to show how you would do it in Golang with iota magic... PS: seems like the unicode strings that I pasted didn't carry over to HNs code snippet. The same is available on pastebin: https://pastebin.com/Yw0jyGDE https://pastebin.com/Yw0jyGDE
- divan 4y agoI think people mean different things by enums. Is it just a set of constants of a given type or a sum type (aka variant type)? Go doesn't have sum types at all. `iota` is just a syntax sugar for enumerating integer constants/variables. But as you're talking about string enums, the Go equivalent would be: type Season string const ( Summer Season = "Summer" Autumn = "Autumn" Winter = "Winter" Spring = "Spring" ) But, again, that's not a variant type, so you can still assign a different value to the variable of the type Season. Which is rarely a problem in practice (there are other ways to validate data rather than by type system constraints). So just to clarify, many of the use cases for enumerated values do not need string representation. Go has no magical way to translate varibale/const name into the string.
- noisy_boy 4y agoBy enum I mean this: > An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. [0] > Is it just a set of constants of a given type or a sum type (aka variant type)? So by above definition, it is a set of predefined constants. > But, again, that's not a variant type, so you can still assign a different value to the variable of the type Season. Which kinda defeats the purpose doesn't it? > Which is rarely a problem in practice (there are other ways to validate data rather than by type system constraints). I'm not sure I agree that it is a rare problem - if I can't rely on the type system to do a check that something belongs to a set of predefined constants, that is a pretty basic part missing. Of course there would be other ways but being able to lean on the type system would be a pretty reasonable expectation. > So just to clarify, many of the use cases for enumerated values do not need string representation. Sure - just like many of the use cases do require string representation. It is not unreasonable to expect that enumerations as defined above should be able handle cases that do and don't require string representation. > Go has no magical way to translate varibale/const name into the string. You keep using this word magical when it comes to addressing the limitations of the language. Java isn't doing "magic" when it is able to return a string representation of the enum value - it is part of the language. Credit where credit is due etc. [0]: https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html https://docs.oracle.com/javase/tutorial/java/javaOO/enum.htm...