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);
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?
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.
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).
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:
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.
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.
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.
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.
You missed the point as well. You can't define a union type in Rust that is backed by a custom enum type. In rust it's a single conflated type. I can't make a Person enum that is backed by a PersonKind. I don't know how else to describe this. I'm not sure how else to explain this.
Ever since I started riding a motorcycle I've started to believe this to be the case too. I get so many bug splats on my helmet, jacket, and motorcycle that I'd never have if I was driving a car. By the end of most rides out in the country on the highway I'm covered in splats.
I added it to my cart 2 minutes before and spent 3 hours trying before realizing that just because it's in your cart doesn't mean it exists since it was actually out of stock.
It's crazy how unusable most gun websites are for browsing what's available. This though is the perfect example of what I really want when browsing catalogues.
No? Not everyone's dream is being a manager. I like writing code, it's fun! Telling someone else to go write code for me so that I can read it later? Not fun, avoid it if possible (sometimes it's unavoidable, we don't have unlimited time).
I meant what we want from an economical perspective, scalability wise. I agree writing code is fun and even disabled AI autocomplete because of it... But I fear it may end up being how we like making our own bread
People still play chess, even though now AI is far superior to any human. In the future you will still be able to hand-write code for fun, but you might not be able to earn a living by doing it.
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?
reply