Thread overview
nested enum
Aug 24, 2016
Illuminati
Aug 25, 2016
Mike Parker
Aug 25, 2016
Daniel Kozak
Aug 25, 2016
Illuminati
Aug 25, 2016
Cauterite
Aug 25, 2016
Daniel Kozak
Aug 25, 2016
Cauterite
Aug 25, 2016
Seb
August 24, 2016
How can I create nested enum like structures?

instead of Enum.X_Y I would like to access like Enum.X.Y

Yet I want it to behave exactly as an enum.  I just want to not use _ as .'s are better as they express more clearly what I want.


August 25, 2016
On Wednesday, 24 August 2016 at 23:04:25 UTC, Illuminati wrote:
> How can I create nested enum like structures?
>
> instead of Enum.X_Y I would like to access like Enum.X.Y
>
> Yet I want it to behave exactly as an enum.  I just want to not use _ as .'s are better as they express more clearly what I want.

struct MyEnum {
    enum X { Y = 10, Z = 20 }
}

void main() {
    import std.stdio;
    int y = MyEnum.X.Y;
    writeln(y);
}
August 25, 2016
And if you need more levels:

struct MyEnum {

    static struct AnotherEnum {

        enum X { Y = 10, Z = 20 }

    }

}

void main() {
    import std.stdio;
    int y = MyEnum.AnotherEnum.X.Y;
    writeln(y);
}

Dne 25.8.2016 v 03:37 Mike Parker via Digitalmars-d-learn napsal(a):
> On Wednesday, 24 August 2016 at 23:04:25 UTC, Illuminati wrote:
>> How can I create nested enum like structures?
>>
>> instead of Enum.X_Y I would like to access like Enum.X.Y
>>
>> Yet I want it to behave exactly as an enum.  I just want to not use _ as .'s are better as they express more clearly what I want.
>
> struct MyEnum {
>     enum X { Y = 10, Z = 20 }
> }
>
> void main() {
>     import std.stdio;
>     int y = MyEnum.X.Y;
>     writeln(y);
> }

August 25, 2016
On Wednesday, 24 August 2016 at 23:04:25 UTC, Illuminati wrote:
>

Well those other answers aren't wrong, but I envisioned that you'd have multiple categories within your sub-enums and whatnot, so you'd need something more like this:

struct A {
	enum X {
		one,
		two,
		three,
	};
	enum Y {
		four = X.max + 1,
		five,
		six,
	};
	enum Z {
		seven = Y.max + 1,
		eight,
		nine,
	};
};

Continuing each enumeration from the end of the previous ensures you won't get any fields with the same values.
August 25, 2016
Btw, tehre is no need for extra semicolon (`;`) after enum and struct definition


Dne 25.8.2016 v 12:23 Cauterite via Digitalmars-d-learn napsal(a):
> On Wednesday, 24 August 2016 at 23:04:25 UTC, Illuminati wrote:
>>
>
> Well those other answers aren't wrong, but I envisioned that you'd have multiple categories within your sub-enums and whatnot, so you'd need something more like this:
>
> struct A {
>     enum X {
>         one,
>         two,
>         three,
>     };
>     enum Y {
>         four = X.max + 1,
>         five,
>         six,
>     };
>     enum Z {
>         seven = Y.max + 1,
>         eight,
>         nine,
>     };
> };
>
> Continuing each enumeration from the end of the previous ensures you won't get any fields with the same values.

August 25, 2016
On Thursday, 25 August 2016 at 10:36:21 UTC, Daniel Kozak wrote:
> Btw, tehre is no need for extra semicolon (`;`) after enum and struct definition

Thanks. This forum insists on reminding me every time I write code here.
August 25, 2016
On Thursday, 25 August 2016 at 11:09:43 UTC, Cauterite wrote:
> On Thursday, 25 August 2016 at 10:36:21 UTC, Daniel Kozak wrote:
>> Btw, tehre is no need for extra semicolon (`;`) after enum and struct definition
>
> Thanks. This forum insists on reminding me every time I write code here.

Warning about this is now an enhancement request: https://issues.dlang.org/show_bug.cgi?id=16430
August 25, 2016
On Thursday, 25 August 2016 at 01:37:05 UTC, Mike Parker wrote:
> On Wednesday, 24 August 2016 at 23:04:25 UTC, Illuminati wrote:
>> How can I create nested enum like structures?
>>
>> instead of Enum.X_Y I would like to access like Enum.X.Y
>>
>> Yet I want it to behave exactly as an enum.  I just want to not use _ as .'s are better as they express more clearly what I want.
>
> struct MyEnum {
>     enum X { Y = 10, Z = 20 }
> }
>
> void main() {
>     import std.stdio;
>     int y = MyEnum.X.Y;
>     writeln(y);
> }

Thanks. I should have thought of that.