www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - nested enum

reply Illuminati <Joe Masons.com> writes:
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.
Aug 24 2016
next sibling parent reply Mike Parker <aldacron gmail.com> writes:
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); }
Aug 24 2016
next sibling parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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); }
Aug 24 2016
prev sibling parent Illuminati <Joe Masons.com> writes:
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.
Aug 25 2016
prev sibling parent reply Cauterite <cauterite gmail.com> writes:
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.
Aug 25 2016
parent reply Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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.
Aug 25 2016
parent reply Cauterite <cauterite gmail.com> writes:
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.
Aug 25 2016
parent Seb <seb wilzba.ch> writes:
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
Aug 25 2016