-
Notifications
You must be signed in to change notification settings - Fork 0
Description
Description
Languages like Rust support the enclosure of an algebraic type within an enum. This has the advantage that at compile time you can match the variant and know what the type is. Allowing for a form dynamic polymorphism with compile time guarantees.
Structurally all variants of the enum are the size of the largest variant structure plus the variant ID. If all variants are empty then the struct then it is only the size of the variant ID, hence behaving the same as a more standard enum implementation.
This design is a lot more powerful than a standard enum, as it allows the encapsulate the data representing state instead of just a label indicating state. It's use in polymorphic code cannot be understated either. Allowing for something akin to interfaces or type unions.