I explored ideas along these lines (as well as others) a couple years ago here: Value Subtypes and Generalized Enums, a manifesto.
I think the inheritance-based syntax is not the right thing to do here as the relationship is backwards from what most people seem to intuitively expect. You actually got it right here (aside from leaving off the associated types in Bar
), but based on previous discussions I've seen most people want the syntax to be the other way around - saying Foo: Bar
and then just declare the new cases.
The syntax I used in that gist is:
enum Sub {
case one
case two
}
enum Super {
// Sub is defined elsewhere, it could even be in a different module if it is closed.
cases Sub
case three
case four
// Cannot declare cases named `one` or `two` because `Sub` already declares them.
}
I also showed an inline variant of the syntax:
enum Super {
cases enum Sub {
case one
case two
}
case three
case four
}
One obvious question is whether these enums would have a subtype relationship with the subtype implicitly converting to the supertype. You didn't directly address this in your post, but your use of inheritance-derived syntax seems to imply that.