Remix.run Logo
▲ xeubie 3 hours ago

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.

▲afdbcreid 2 hours ago | parent | next [-]

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 an hour 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....

▲sagarm an hour ago | parent | prev [-]

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.

▲xeubie 10 minutes ago | parent [-]

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.