On Saturday, 19 March 2016 at 17:40:27 UTC, Lass Safin wrote:
Why:

enum Base {
    A,
    B,
}

enum Derived : Base {
C, // Gives error, says it can't implicitly convert expression to Base.
    D = 1, // Same error
E = cast(Base)294, // Finally works. Can only be cast(Derived) instead.
}

void func(Derived d) {}

func(Derived.E); // works.
func(Derived.A); // Gives error, says it can't call function with Base.A.
func(cast(Derived)Derived.A); // Works.

So, what's the proper way of extending an enum?

Look at the grammar:

https://dlang.org/spec/enum.html

There's no inheritance system for the enums. after the ":" can only be specified the type of the members, aka the "EnumBaseType".

"
EnumDeclaration:
    enum Identifier EnumBody
    enum Identifier : EnumBaseType EnumBody
"

So when you write

enum Derived : Base {}

It just means that "Derived" members must be of type "Base"

So actually the only thing you can do is to reduce the members count:

enum Base {A,B}
enum Derived : Base {C = Base.A}



Reply via email to