November 28, 2013
I'd like to define an enum type with only a subset of the fields of another enum.

enum Foo { A, B, C }
enum Bar : Foo { Foo.A, Foo.B }
void main() {
    //Foo f = Bar.A; // Should be OK.
    //Bar b = Foo.A; // Should be an error.
}


I receive the errors:

temp.d(2): Error: type only allowed if anonymous enum and no enum type
temp.d(2): Error: if type, there must be an initializer
temp.d(2): Error: type only allowed if anonymous enum and no enum type
temp.d(2): Error: if type, there must be an initializer


Defining a subset like that is quite useful, it allows to statically avoid some bugs.

An alternative and more clear syntax:

enum Foo { A, B, C }
subset enum Bar : Foo { A, B }
superset enum Spam : Foo { D }
void main() {}


Perhaps something like that can be implemented with Phobos library code (with a worse syntax)?

Bye,
bearophile