| ▲ | xeubie 3 hours ago |
| Rust enums conflate the tag type with the union type that uses it. These should be separate things. In the example here, you can see that Rust gives no way to represent the tag type `{Human, Cat}`. In Zig there is an enum which is a set of tags, and a union which is backed by that enum and can have fields. It is obviously better to have these as separate concepts rather than conflating them. You often want the kind without the data, and Rust gives you no way to represent it. I'm not sure why I am bothering to rant about this but it's one of my biggest annoyances with the language. |
|
| ▲ | clownstrikelol 3 hours ago | parent | next [-] |
| Sure you can, bringing data along for the ride is optional in Rust. Nothing is forcing you to use it like a tagged union, it’s an optional (but useful) feature. If you want specific values (integer or otherwise) associated with specific variants (i.e. Human = 1, Cat = 4) you can do that too. Use the #[repr(T)] or impl the From / Into traits yourself. |
| |
| ▲ | xeubie 3 hours ago | parent [-] | | No, you can't, because you're misunderstanding my point. You can make an enum in rust that doesn't contain data, but you can't make an enum that is explicitly backed by a tag type. `#[repr(u8)]` lets the tag be a u8 with values like Human = 1, Cat = 4, but you can’t say "Person's tag is my PersonKind enum." Zig lets you do exactly that with `union(PersonKind)`. In Rust, PersonKind would always be a separate type you keep in sync by hand. |
|
|
| ▲ | ai_ 2 hours ago | parent | prev | next [-] |
| The function you seek already exists before you:
https://doc.rust-lang.org/std/mem/fn.discriminant.html Is there anything you can do with separate types for the tag and the union that std::mem::discriminant and pattern matching can't do? |
| |
| ▲ | Jtsummers 2 hours ago | parent | next [-] | | As someone who hasn't used Rust enough, and not knowing Zig but it appears Zig does something similar to Ada which I do know, how would you use the same discriminant across multiple Rust enums and guarantee that you implemented variants for each discriminant value? In Ada, if I use an enum (carrying the example from a link I used in a cousin comment): type Expr_Kind_Type is (Bin_Op_Plus,
Bin_Op_Minus,
Num);
https://learn.adacore.com/courses/intro-to-ada/chapters/more...Variant records using this enum have to implement the variant for each element of the enum. If I, for some reason, have multiple records using the same discriminant I can know that each one implements it for each value of the discriminant. Does Rust have a way to synchronize your enums similar to that? | | |
| ▲ | bombela 35 minutes ago | parent | next [-] | | pub trait Payloads {
type Read;
type Write;
}
pub enum FileOp<P: Payloads> {
Read(P::Read),
Write(P::Write),
}
pub struct Inputs;
impl Payloads for Inputs {
type Read = ();
type Write = u8;
}
pub type Request = FileOp<Inputs>;
pub struct Outputs;
impl Payloads for Outputs {
type Read = u8;
type Write = ();
}
pub type Reply = FileOp<Outputs>;
pub fn execute(request: Request) -> Reply {
match request {
Request::Read(()) => Reply::Read(42),
Request::Write(_) => Reply::Write(()),
}
}
| | |
| ▲ | Jtsummers 23 minutes ago | parent [-] | | This looks like it would handle synchronizing multiple variant records/enums, and if you just wanted the "plain" enum without any payload, you'd need to do: pub struct Bare;
impl Payloads for Bare {
type Read = ();
type Write = ();
}
pub type BareFileOp = FileOp<Bare>
Correct? So you'd need to create it with BareFileOp::Read(()), but at least it wouldn't carry any overhead of actually containing data. |
| |
| ▲ | afdbcreid an hour ago | parent | prev [-] | | Why do you need to do that? You can't be generic over "any enum using this tag" anyway (in Rust, at least). If you just want to make sure you make no mistake, you can use a dead match from the tag to the enum, or automate it with a macro, and the compiler will enforce exhaustiveness (though not on both sides, unless you use a macro). | | |
| ▲ | Jtsummers an hour ago | parent [-] | | If I'm building up a type checker and an interpreter, I very well could (continuing that example) end up with different data structures using the same discriminant (but containing different data about the system). In Rust I'd have to do something like: enum Parser {
bin_add(...),
bin_sub(...),
...
}
enum Typer {
bin_add(...),
bin_sub(...),
...
}
And I'd have to hope they were both complete and match (at least according to what I know now, that's why I'm asking). If I go back in to the Parser enum and add a `un_minus`, then I have to also go into the Typer enum (apologies for the bad naming, I'm at work and commenting between my way too many meetings) and manually add it. I have no compile time guarantee that both are complete and synchronized. In Ada, I modify the existing enum and both will fail to compile until I add in the variant for `un_minus`. One edit, multiple failures distributed across the code base as a result telling me all the places where I have to add in a new code branch or new variant.EDIT: Run on sentence broken up. | | |
| ▲ | bombela 33 minutes ago | parent [-] | | You can use one generic enum with a trait holding the payload type. I posted an example as response to your first comment. |
|
|
| |
| ▲ | tcfhgj an hour ago | parent | prev | next [-] | | yeah, but fortunately strum exists. I have found it quite useful to be able to generate a dedicated SomeEnumDiscriminants enum for SomeEnum, using the following derive: https://docs.rs/strum/latest/strum/derive.EnumDiscriminants.... | |
| ▲ | xeubie 2 hours ago | parent | prev [-] | | Yes, there is something you can do with separate types. You can name a variant without having a value of it. `discriminant` only works on an existing value, so to refer to "cats" you'd have to build a dummy `Cat`, and the result is opaque. You can't match on it, iterate over it, or create one from a byte you parsed off the wire. This is so obviously worse, I can't believe you bothered to ask. | | |
| ▲ | sagarm 17 minutes ago | parent | next [-] | | You design the enum. If you need kinds separate from the union itself, you have that power. The union type always implies a kind, after all, so it's a generalization of the concept. | |
| ▲ | afdbcreid an hour ago | parent | prev [-] | | This is a minor advantage (but is an advantage) and when needed (rarely) you can usually workaround with a different fieldless enum and potentially a proc-macro to autogenerate it. | | |
| ▲ | xeubie 43 minutes ago | parent | next [-] | | It's not at all minor. Sometimes the tag enum already exists. Rust can't build a data enum on top of it: a derive macro only sees its own enum, so it generates a new tag enum instead. Now you have two tag enums to keep in sync by hand. Proc macros are always used to resolve deficiencies in the rust language, and they always lead to greater complexity and longer compile times. | |
| ▲ | tcfhgj an hour ago | parent | prev [-] | | > proc-macro to autogenerate it. https://docs.rs/strum/latest/strum/derive.EnumDiscriminants.... |
|
|
|
|
| ▲ | 3 hours ago | parent | prev | next [-] |
| [deleted] |
|
| ▲ | slopinthebag 3 hours ago | parent | prev [-] |
| i dont understand what rust doesn't give you that zig does? |
| |
| ▲ | Jtsummers 3 hours ago | parent [-] | | https://zig.guide/language-basics/unions/ - Seems like it's more similar to Ada's variant records: https://learn.adacore.com/courses/intro-to-ada/chapters/more... Instead of just getting one type that includes the discriminant and records, Zig and Ada have a tag of some type whose value determines which variant is being used, and then the union (Zig) or record (Ada) is its own type. | | |
| ▲ | slopinthebag an hour ago | parent | next [-] | | why would you ever need them to be seperate? | | |
| ▲ | Jtsummers 41 minutes ago | parent | next [-] | | To use the discriminant type on its own without carrying the baggage of the entire record and all its data, or to use the discriminant type across multiple records rather than creating some mega-record or manually synchronizing them. In Ada (again, don't know Zig but from the docs it's similar to what Ada is doing), adding a new value to the discriminant enum would cause a compile time error in all the variant records using it and all the case statements (match, but Ada's version), as long as they didn't have a catchall branch. | |
| ▲ | 12_throw_away an hour ago | parent | prev [-] | | Because sometimes you just want to talk about the types of variants without having to construct a value. But, you probably don't want to manually maintain a separate enum just for the discriminants. Nor do you want to manually maintain the mappings between the original enum and the discriminant-only enum. I've had it come up when dealing with ASTs, although I don't recall the specifics. This is a problem that's certainly pretty easy to solve with macros. I'm pretty sure last time I ran into this I used `strum` [1] and its `EnumDiscriminants` macro to handle this. [1] https://lib.rs/crates/strum | | |
| ▲ | slopinthebag an hour ago | parent [-] | | ahh ok ive run into this before, yes either strum or i assumed i was doing something non-optimal if i had to talk about types like this |
|
| |
| ▲ | knorker 2 hours ago | parent | prev [-] | | I still don't get it. Is it like a Rust enum, but worse, because you can accidentally only detect the problem at runtime? |
|
|