5 ms·
I always hated the name. You're not enumerating anything. You are picking a variant, encoding a choice. Maybe "sum type" is too "monad", but surely "variant rec
by bruce343434 3y ago
I always hated the name. You're not enumerating anything. You are picking a variant, encoding a choice. Maybe "sum type" is too "monad", but surely "variant record" or even "variant" would have been a better name.
- hurril 3y agoWhile I agree with you, I can understand going with enum so that they don't scare away the C and C++ crowd with being too fartsy with theoretic stuff.
- MuffinFlavored 3y agothey scared me away when I tried to do from/to u32 and comparing them, etc.
- flohofwoe 3y agoIt is especially confusing for C and C++ coders, because Rust enums are a completely different thing than C enums, which makes the naming choice worse than just inventing a new name. Better names would be "tagged union" (which is spot-on descriptive - because it *is* a union with a tag slapped on - but "tagged_union" of course is a bit unwieldy as keyword) or "variant" (which is the name for that thing in C++ and elsewhere since forever - it's not like Rust invented those tagged-union-thingies, it just added some convenient syntax sugar to the language to deal with them).
- jowea 3y agoMaybe it's because you can use a Rust enum like in C by doing a field-less or unit-only enum?
- hurril 3y agoI agree with you! I dislike the name enum and really like how F# does it. In F#, everything is type and what kind of type you create comes down to the type construction literal you use. ` (* enum ) type 'a Option = Some of 'a | None ` ( struct - well: record. *) type Company = { Name : string Age : int } Fudge. No idea how to format that in HN.
- randomdata 3y agoIn practice, now C developers get to hear from the Rust crowd that C doesn't have enums because what C has is unlike Rust "enums".
- kibwen 3y agoIn 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.
- TwentyPosts 3y ago"coproduct type"
- math_dandy 3y agoOCAML uses the terminology “(Polymorphic) Variant” for this kind of thing. Since OCAML is awesome, I consider this a strong precedent.
- dgb23 3y agoThey are tagged unions right? "Enum" is not the worst term here. In fact you are enumerating the variants. It's only slightly misleading. It implies that you're comparing an integer. There is an actual enum backing it to encode the choice (tag), and a union to encode the data (if it has data). Or at least that's what I would expect. "Variant" is used in Rust lingo to name the variants, not the whole type.
- layer8 3y agoSome schema languages call it “choice”.