Managing enum subsets


#1

Currently if you want to handle subsets of enums you have to use something like:

enum Pet { Dog, Cat, Fish }
enum Animal { Bear, Giraffe, Other(Pet) }

fn foo(x: Animal) {}
fn main() {
    foo(Animal::Other(Pet::Cat));
}

But if subsets of enums are used very commonly, then another way to handle them could be:

enum Pet { Dog, Cat, Fish }
enum Animal = Pet + { Bear, Giraffe }

fn foo(x: Animal) {}
fn main() {
    foo(Pet::Cat);
}

Even subtractions:

enum Animal { Dog, Cat, Fish, Bear, Giraffe }
enum Pet = Animal - { Bear, Giraffe }

#2

Keep in mind that enum provides a namespace, so Pet::Dog is a different thing than Animal::Dog.
For that reason I’d go with first option (for clarity you can name it just Pet, so you’ll get:
enum Animal { Bear, Giraffe, Pet(Pet) }

to get such enum algebra working, you may look at macros (I don’t know if they can do that).


#3

Right, this is a proposal for an improvement in the management of those namespaces.
Macros are not a solution to every type system idea I suggest around here :slight_smile: