3 ms·
The syntax and terminology (enums, structs, etc.) is more familiar to C++ / C# / Java users.
by roar-of-time 7y ago
The syntax and terminology (enums, structs, etc.) is more familiar to C++ / C# / Java users.
- Athas 7y agoI always found the use of "enum" for things that are not really enumerable in a useful way to be very confusing. Or are Rust "enum"s enumerable in some subtle way that I don't recognise? Is it just some vestigial term that now has no relation to its original meaning? At least in C, "enum"s are enumerable because they are just integers.
- loeg 7y agoRust's "enums" look more like tagged unions to me. I guess the tag is enumerable? Although I also don't understand why Rust called tagged unions "enums."
- steveklabnik 7y agoThey enumerate a possible set of valid values. Hence “enumeration.” They are tagged unions, but sometimes, the tag doesn’t exist. Or rather, invalid parts of values can be used so that the tag isn’t an extra bit of data, but instead is built into the same space. “Tagged union” gets too deep into only-mostly-accurate implementation details to be a good name.
- loeg 7y agoThey seem to enumerate a possible set of valid structures which can hold arbitrary values. I guess it's just so different from C enums I'm having trouble understanding why the name was repurposed. It's probably less different from C++/C#/Java enums (I know at least one/some of those languages have more complicated enums than C). Sure, tagged union implies a particular implementation that may not always be required, but it's conceptually easy to understand and doesn't have the historical baggage. (Maybe a better description would be strongly typed union? I want to make clear I'm unfamiliar with Rust and just guessing based on the syntax presented.) I think the biggest problem with "tagged union" (or the even longer "strongly typed union") is that it just isn't a good keyword name — it's two (or three) words and fairly long. No one wants to type out 'tagged_union' and from that sense, 'enum' is better. I don't have a better suggestion for you, and IIRC Rust 1.0 has now frozen the language to some extent. Thanks for trying to explain, I appreciate it.
- steveklabnik 7y agoThey can be any data type; just a name, a struct, a tuple struct, or even another enum. I think also, likewise, “union” sounds strange unless you have C experience. Many of our users do not know C, and so that name doesn’t help them either. In the end names are hard. Happy to, you’re very welcome :)
- loeg 7y ago> In the end names are hard. Indeed! Thanks again.
- roar-of-time 7y agoC, or a little set theory. :)
- hope-striker 7y agoThey enumerate a finite set of disjoint cases, so in some sense they are an enumeration. But the real reason is, of course, that sum types can be seen a generalization of C enums, so the syntax was chosen to maximize familiarity.
- gowld 7y agoCases aren't values, though. a,b,c is an enum type over enumerable data. int, char, float is an enum kind of enumerable types.