StardustPL / Stardust

"A programming language that doesn't make me angry to use it."
https://StardustPL.GitHub.IO/
The Unlicense
4 stars 0 forks source link

Planning: enums #16

Open LB-- opened 9 years ago

LB-- commented 9 years ago

Normal enumerations are just as in other languages (e.g. Java). The enumeration is a distinct type with all instances known at compile time. A function which has a parameter of the enum type will accept any of the enum values. However, each value of the enum is actually a singleton of a derived type unique to it. A function can accept a specific enum value type rather than the generic catch-all base.

In fact, really an enum is a tree of types where the leafs are singletons and the the rest are abstract.

Example enums with WIP syntax:

enum test1 //test1 is a singleton value with its own type

enum test2: //test2 is an abstract base class
    red //single of unique type that derives test2
    green //ditto
    blue //ditto

enum test3: //abstract
    default //concrete
    fine: //abstract
        verbose //concrete
        terse //concrete
    rough: //abstract
        errors //concrete
        warnings //concrete
        both /concrete

Enum instances can also have members specified and can be initialized specially if they have constructors, though I've not yet decided on the syntax for it.

LB-- commented 8 years ago

Actually, I think I prefer Rust-style enums (e.g. to implement optional).

LB-- commented 8 years ago

Why not both? Categories and enumerations. Enums could even support the nesting with polymorphic types. Might not even need two separate things at all.