High-Level Parameterized Enums

So there's a neat feature in Haskell and Rust (and other languages) which is that you can make an enumerated type which takes parameters. Here's Rust's version:

enum Message {
    Quit,
    ChangeColor(i32, i32, i32),
    Move { x: i32, y: i32 },
    Write(String),
}

In C++ there's not built-in support for this. You have to implement it as a "tagged union"...where you have a struct that has a plain enum integer in it, and then a union of all the possibilities of what could be in that spot.

So this made me wonder what we could do to support it.

Fans of History May Recall I Made an Enumerated Type Once

I wasn't much involved with the language until it was open-sourced. But I did tinker with it in 2009

>> fruit: make-enum-type [apple orange banana mango]

>> favorite_fruit: make-enum fruit 'apple

>> set-enum favorite_fruit 'shoe
** User Error: illegal enum value ( shoe ) when 
     possibilities are [ apple orange banana mango ]

Clearly we want better than that, which might look more like:

fruit: new enum [apple [rotten: [logic!]] orange banana mango]

favorite-fruit: new fruit.apple false

It raises some questions about how SWITCH might be able to be hooked to have some augmented feature, where it enforces you covering all the cases for the ENUM.

I don't have any great ideas offhand...but I just was hearing a talk where this was brought up and it reminded me that maybe it would be a good example to try and think through, as a feature users could add after-the-fact.

2 Likes