Skip to content

Feature: Algebraic enums #2

@shortercode

Description

@shortercode

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions