digitalmars.D.learn - nested enum
- Illuminati (4/4) Aug 24 2016 How can I create nested enum like structures?
- Mike Parker (9/14) Aug 24 2016 struct MyEnum {
- Daniel Kozak via Digitalmars-d-learn (12/27) Aug 24 2016 And if you need more levels:
- Illuminati (2/18) Aug 25 2016 Thanks. I should have thought of that.
- Cauterite (23/23) Aug 25 2016 On Wednesday, 24 August 2016 at 23:04:25 UTC, Illuminati wrote:
- Daniel Kozak via Digitalmars-d-learn (3/27) Aug 25 2016 Btw, tehre is no need for extra semicolon (`;`) after enum and struct
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
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
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
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:Thanks. I should have thought of that.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 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.
Aug 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.
Aug 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 definitionThanks. This forum insists on reminding me every time I write code here.
Aug 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:Warning about this is now an enhancement request: https://issues.dlang.org/show_bug.cgi?id=16430Btw, tehre is no need for extra semicolon (`;`) after enum and struct definitionThanks. This forum insists on reminding me every time I write code here.
Aug 25 2016