Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
August 24, 2016 nested enum | ||||
---|---|---|---|---|
| ||||
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 Re: nested enum | ||||
---|---|---|---|---|
| ||||
Posted in reply to Illuminati | 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 Re: nested enum | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | 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 Re: nested enum | ||||
---|---|---|---|---|
| ||||
Posted in reply to Illuminati | 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 Re: nested enum | ||||
---|---|---|---|---|
| ||||
Posted in reply to Cauterite | 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 Re: nested enum | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | 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 Re: nested enum | ||||
---|---|---|---|---|
| ||||
Posted in reply to Cauterite | 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 Re: nested enum | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | 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.
|
Copyright © 1999-2021 by the D Language Foundation