logoalt Hacker News

xeubie • yesterday at 10:22 PM • 2 replies • view on HN

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.


Replies

sagarm • today at 12:08 AM

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 • yesterday at 11:07 PM

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.

➕ show 2 replies