5 ms·
Does anyone here know why they call those structures Enums rather than Unions?
by Symmetry 13y ago
Does anyone here know why they call those structures Enums rather than Unions?
- doublec 13y agoI think it's for familiarity for C++ programmers: enum Day { Monday, Tuesday, Wednesday, ... } You can assign values too: enum Color { Red = 0xff0000, Green = 0x00ff00, Blue = 0x0000ff }
- Daishiman 13y agoThe Rust Enum is a more generalized data structure, which aside from being able to provide your usual Enum functionalities, can also contain members with type parameters. The cool thing is that all this functionality is accessible through the match() syntax, which exhaustively checks all possible forms the Enum can take, thus making data access completely safe.
- chongli 13y agoIt's rather infuriating. An Enum is supposed to be just a Union of nullary constructors. What they're doing here is using the word Enum when they really ought to mean Union. This sort of overloading of language happens all over the place in programming and it leads to endless confusion. I find it extremely frustrating but what can I do about it?
- sixbrx 13y agoI was put off slightly at first by the name as well, not so much now. It is a generalization of the enums from the C languages. Also Enum seems a good name because they represent fixed alternatives that may be (finitely) enumerated (We're saying "Let me enumerate the alternatives that this type may hold."). Whereas "union" seems to speak to its underlying implementation (which may or may not hold) as a tagged, overlapping union. Ie. that usage of union says much less about the intended semantics and is more of a possible implementation detail. Otherwise if you look at the actual meaning of "union" it doesn't seem to fit really. Maybe "sum" might be closer but again that's leaning towards the theoretical, and in that case you'd probably then need to rename struct to become "product" for symmetry.
- chongli 13y agoHey, I'd be all for having them called sum and product types. But then why not go full out and have (generalized) algebraic data types?
- pcwalton 13y agoWe had a long debate about this. Eventually it was decided that "union" would give C programmers the impression that it was about unsafe unions, not tagged unions. Also, the idea of associating data with enums has precedent in Java.