| Thread overview | ||||||
|---|---|---|---|---|---|---|
|
August 13, 2011 enum inheritance? | ||||
|---|---|---|---|---|
| ||||
Hi! I am want to extend available enum to provide more items to them. How I can do this job without manual copying of exsisting enum items? | ||||
August 13, 2011 Re: enum inheritance? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to mimocrocodil | On Saturday, August 13, 2011 23:15:29 mimocrocodil wrote:
> Hi!
>
> I am want to extend available enum to provide more items to them.
>
> How I can do this job without manual copying of exsisting enum items?
You can't. An enum has a fixed set of values. You can't extend it in any way, shape, or form. You can create other enums, but you can't do anything to add new values to the original unless you go and alter the enum declaration to add more values. And if you create a new enum, you can't use it in place of the old enum, because it's not the same enum and doesn't have the same values.
- Jonathan M Davis
| |||
August 14, 2011 Re: enum inheritance? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to mimocrocodil | On Sun, 14 Aug 2011 01:15:29 +0200, mimocrocodil <4denizzz@gmail.com> wrote: > Hi! > > I am want to extend available enum to provide more items to them. > > How I can do this job without manual copying of exsisting enum items? If what you want is a new enum that contains the values of an existing enum, with some additions, this code will do it: string EnumDefAsString(T)() if (is(T == enum)) { string result = ""; foreach (e; __traits(allMembers, T)) { result ~= e ~ " = T." ~ e ~ ","; } return result; } template ExtendEnum(T, string s) if (is(T == enum) && is(typeof({mixin("enum a{"~s~"}");}))) { mixin("enum ExtendEnum {" ~ EnumDefAsString!T() ~ s ~ "}"); } unittest { enum bar { a = 1, b = 7, c = 19 } import std.typetuple; alias ExtendEnum!(bar, q{ // Usage example here. d = 25 }) bar2; foreach (i, e; __traits(allMembers, bar2)) { static assert( e == TypeTuple!("a", "b", "c", "d")[i] ); } assert( bar2.a == bar.a ); assert( bar2.b == bar.b ); assert( bar2.c == bar.c ); assert( bar2.d == 25 ); static assert(!is(typeof( ExtendEnum!(int, "a")))); static assert(!is(typeof( ExtendEnum!(bar, "25")))); } -- Simen | |||
August 14, 2011 Re: enum inheritance? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Simen Kjaeraas | On 8/13/11 9:42 PM, Simen Kjaeraas wrote:
> On Sun, 14 Aug 2011 01:15:29 +0200, mimocrocodil <4denizzz@gmail.com>
> wrote:
>
>> Hi!
>>
>> I am want to extend available enum to provide more items to them.
>>
>> How I can do this job without manual copying of exsisting enum items?
>
> If what you want is a new enum that contains the values of an existing
> enum,
> with some additions, this code will do it:
>
>
> string EnumDefAsString(T)() if (is(T == enum)) {
> string result = "";
> foreach (e; __traits(allMembers, T)) {
> result ~= e ~ " = T." ~ e ~ ",";
> }
> return result;
> }
>
> template ExtendEnum(T, string s) if (is(T == enum) &&
> is(typeof({mixin("enum a{"~s~"}");}))) {
> mixin("enum ExtendEnum {" ~
> EnumDefAsString!T() ~ s ~
> "}");
> }
Has D considered introducing string interpolation? That looks very hard to read...
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply