Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Nested enums #14720

Closed
sirgru opened this issue Oct 25, 2016 · 8 comments
Closed

Proposal: Nested enums #14720

sirgru opened this issue Oct 25, 2016 · 8 comments

Comments

@sirgru
Copy link

sirgru commented Oct 25, 2016

I would find it convenient to have something like this (contrived example):

enum Animal
{
    Mammal
    {
        Cat,
        Dog,
        Bear
    },
    Bird
    {
        Pigeon,
        Hawk,
        Ostrich
    },
    Fish
    {
        Goldfish,
        Trout,
        Shark
    }
};

Sometimes I want the class hierarchy but sometimes I really don't. Use case would be something like this:

var creature1 = Animal.Bird.Hawk;
Animal creature2 = Animal.Mammal; //OK

creature1 == Animal.Bird; // false
creature1 is Animal.Bird; // true
creature1 is Animal; // true

@vbcodec
Copy link

vbcodec commented Oct 25, 2016

Barely useful, as you can write

enum Animal
{
    Mammal_Cat,
    Mammal_Dog,
    Mammal_Bear
...
};

and with intellisense you get the same benefits.

Beside of this, CLR probably do not allow for these enhancements, and here must be implemented significant trickery to smoothly move these enums between sources and libraries.

@sirgru
Copy link
Author

sirgru commented Oct 26, 2016

Well, the intellisense benefits are of no use to me, since I wanted to react differently based on the enum's value and "parent value" at runtime. This is the use case very similar to what I have in mind, and yes the solutions are "ugly" with "muddy intent".
http://stackoverflow.com/questions/980766/how-do-i-declare-a-nested-enum

As for the CLR not allowing it smoothly, that I can understand.

@Unknown6656
Copy link

You could achieve this in F# with discriminated unions, but I would love to see this made possible in C# also

@HaloFour
Copy link

Check out #6739.

Normal C#/CLR enums can only be simple integral primitive types. Short of getting funky with bitmasks I don't see how a hierarchical relationship could be established through them. Discriminated unions would be a better feature.

@jveselka
Copy link

jveselka commented Nov 1, 2016

This can be represented by flags enum reasonably well

[Flags]
enum Animal
{
    //Groups
    Mammal = 0x1,
    Bird = 0x2,
    Fish = 0x4,
    //Mammals
    Cat = 0x101,
    Dog = 0x201,
    Bear = 0x401,
    //Birds
    Pigeon = 0x102,
    Hawk = 0x202,
    Ostrich = 0x402
    //Fishes
    Goldfish = 0x104,
    Trout = 0x204,
    Shark = 0x404,
};

You can check for presence in group by calling var isBird = someAnimal.HasFlag(Animal.Bird);.
Also with binary literals in C#7, this will be even easier to write.

@svick
Copy link
Contributor

svick commented Dec 28, 2016

This looks very similar to #13192.

@lmcarreiro
Copy link

There is a similar request in dotnet/csharplang#587

@CyrusNajmabadi
Copy link
Member

Closing this out. We're doing all language design now at dotnet/csharplang. If you're still interested in this idea let us know and we can migrate this over to a discussion in that repo. Thanks!

@CyrusNajmabadi CyrusNajmabadi closed this as not planned Won't fix, can't repro, duplicate, stale Nov 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants