4 ms·
In early Rust, the keyword was `tag`. In order to improve familiarity with C++ programmers, bikeshedding raged between `enum` and `union` for years before final
by kibwen 3y ago
In early Rust, the keyword was `tag`. In order to improve familiarity with C++ programmers, bikeshedding raged between `enum` and `union` for years before finally landing on the former.
- flohofwoe 3y agoTBH it's kinda funny how they narrowed it down to two options and then went with the "obviously" wrong option (also to get C++ programmers on board, wouldn't the obvious name not be "variant"? I really wonder how that decision making progress looked like :)
- jowea 3y agoUnion did end up meaning another thing that is just like C union and just as unsafe.
- steveklabnik 3y agoDon’t forget rust also has unions. Not using the name “union” for a feature that’s different than unions doesn’t feel “obviously wrong” to me.
- sirwhinesalot 3y agoThe right solution was to make union safe with an opt-in way to make it unsafe IMO.
- steveklabnik 3y agoIn concrete terms, what would that look like?
- metaltyphoon 3y agoSyntax wise Zig does it with union(enum). Union and Enums are both concepts directly related to C and can be used separately too.
- mattstir 3y agoThe thing is that accessing an untagged union's data is just inherently unsafe. Say you made a union with a `u8` field and a `bool` field. If you had an instance of it and set its value to `5`, that's just not a valid `bool`. Hence the requirement of `unsafe`; you need to tell the compiler that you _know_ it's a valid value at the point you're accessing it.
- sirwhinesalot 3y agoThen make them tagged, that's what rust enums are, tagged unions, except they're called enums instead of unions.
- mattstir 3y agoI mean... yeah, that's exactly why Rust has tagged unions as well... both are useful in the right contexts, but tagged unions are easier to work with in general. That doesn't mean that (untagged) unions should be safe to work with though, because they can't be.
- binary132 3y agoRust's Union is for C FFI, right? Why not call Enum Union, and the FFI one CUnion or something? Like, is there something meaningful that Union is for other than C interop? Isn't it functionally identical to a tagged union in every other respect?
- steveklabnik 3y agoC FFI is a very useful thing, sure, but I don’t think it’s exclusive to that. It’s a nice primitive to have access to. I think Rust would feel weird with tagged unions and not untagged ones, just like having both sum and product types are good.
- tialaramex 3y ago> Rust's Union is for C FFI, right? No :) > is there something meaningful that Union is for other than C interop? Look more closely at the Rust standard library, MaybeUninit<T> is a union.
- Liquid_Fire 3y ago> to get C++ programmers on board, wouldn't the obvious name not be "variant"? The use of "enum" in Rust predates the standardization of C++17, where std::variant was introduced.
- flohofwoe 3y agoBoost as "stdlib prototype" had that type much earlier though, going back to 2002 or so: https://www.boost.org/doc/libs/1_64_0/doc/html/variant.html https://www.boost.org/doc/libs/1_64_0/doc/html/variant.html The name and concept was also common enough to that I used "Variant" in my own C++ utility class collection around 2005 or so.
- ReleaseCandidat 3y agoYes, but `boost::variant` existed since 2004 https://www.boost.org/users/history/version_1_31_0.html https://www.boost.org/users/history/version_1_31_0.html
- kibwen 3y ago> the "obviously" wrong option But it's not, hence the aforementioned bikeshedding. When Rust enum variants have data, they're like C unions with automatically-inserted tag checking. When Rust enums don't have data, they're like C enums (they're literally how you do FFI interop with enums in C, which should demonstrate beyond a shadow of a doubt that `enum` isn't categorically wrong). They have the properties of both depending on how they're defined, but at the end of the day they technically have more in common with C enums than C unions (which is why Rust also has `union` for doing C FFI). > to get C++ programmers on board, wouldn't the obvious name not be "variant"? Most early Rust contributors were also C++ programmers (due to Mozilla), and I don't recall a single person ever suggesting `variant` as the keyword. I suspect people here are overestimating the mindshare of boost::variant as of 2014.
- binary132 3y ago"C++ calls it variant." "OK, let's not go with that then" "C calls it union" "OK, we DEFINITELY can't call it that"
- mattstir 3y agoRust already has things called unions, which are C-style unions. Using the same word for two different things would definitely have been more confusing, not less...