4 ms·
Probably for the same reason that they call their unions "enums" or their heap blocks "boxes". Standardization of jargon has never been a thing with rust, just
by ajross 12y ago
Probably for the same reason that they call their unions "enums" or their heap blocks "boxes". Standardization of jargon has never been a thing with rust, just roll with it.
- steveklabnik 12y agoWell, enums aren't unions, they're tagged unions. And other languages call boxed values by that name too.
- ajross 12y agoStop it, they're unions. :) Among the target market, people are going to intimately familiar with the use of "enum" and "union" from the C language. Rust's concept of a single object that can store exactly one of several types of sub-objects matches one quite closely, and not the other. Having a runtime tag and affirmative checking doesn't change the nature of the thing. We don't call "cars" something different when we add cruise control or anti-lock brakes. "Enumerate" in English is just a fancy word for "count" -- it means to assign numbers to a bunch of things. Which is exactly what the C concept did. The Rust usage (to mean "something that can be in one of a few different states") is new, though Java has something fairly close too. It was a poor choice, sorry. Likewise being deliberately difficult with "trait" vs. "interface" (picking Self's jargon instead of the term that literally everyone already knows from decades of OOP) didn't serve you well. Thus we have blog posts like this needing to tell us what we probably should have been able to figure out from context. Finally, regarding "box" vs. "block". Other languages (C# is the only one that comes to mind off-hand) have used the idea of "boxing" to imply the allocation of space for and copying of pass-by-reference data. That's sort of a different notion than simple heap allocation, so it sort of gets its own jargon I guess. I didn't complain anyway. But with Rust, a "box" really is used to refer to a dynamic heap block in any context. We sort of already had a perfectly good word for that. Pretentious jargon isn't the worst crime in the world, but I do think Rust seems needlessly complicated in the way it likes to play Shakespeare with existing concepts.
- kinghajj 12y agoBut a Rust enum is equivalent to a C-style one in the basic case. Rust's is just able to do more as well to support the common idiom of tagged unions. Unions themselves cannot be expressed safely in Rust, so it'd do little good IMHO to have different terms for them. I guess you could say that they should have avoid "enum" and went with "data" as in Haskell, but then people would complain that the basic ADTs are just enums and wonder why they can't just use "enum" like before!
- steveklabnik 12y agoI guess we'll have to agree to disagree here. I think what you consider 'pretentious jargon' really depends on what kinds of languages you've used and the way you think about them. I think calling enums unions would have been a very big mistake, as they would mislead C programmers. Even you say that they're 'quite close', but they're not the same thing.
- ajross 12y agoI, uh, don't really follow that logic. A Rust enum isn't even "quite close" to a C enum, yet you used a colliding term. Surely a C programmer who would have been mislead about the fact that a union type is runtime-tagged and checked is going to be very misled that an "enum" has no numeric value and can contain variable state at runtime.
- steveklabnik 12y agoI guess my ultimate point is that _everything_ is a colliding term in some way, with some language. Rust isn't C, so you can't assume that things map 1:1 to C. Names are hard.
- deleted 12y ago[deleted]
- lambda 12y agoA Rust enum does does carry a numeric value, if it doesn't contain any other data (playpen link: http://is.gd/HwGajU http://is.gd/HwGajU) enum Foo { Bar, Baz, Quux, Zot = -1 } fn main() { println!("{} {} {} {}", Foo::Bar as i32, Foo::Baz as i32, Foo::Quux as i32, Foo::Zot as i32); } A Rust enum is pretty much an exact combination of a C enum and union. If it doesn't carry any extra data, then it works just like an enum; if it does carry extra data, it works like a tagged union.
- Dewie3 12y agoAnd I guess by "standardization" you mean use a C-like name, even though it is inaccurate and misleading and the data type declaration is taken from another language family (ML)? (I agree that "enum" is a wrong name to use, but apparently for different reasons.)