version specific enum members

Phil Deets pjdeets2 at gmail.com
Thu Oct 29 23:30:26 PDT 2009


On Fri, 30 Oct 2009 00:03:23 -0500, Daniel Keep  
<daniel.keep.lists at gmail.com> wrote:

>
>
> Phil Deets wrote:
>> Hi, is there a way to add members to an enum based on conditional
>> compilation symbols. I tried
>>
>> enum Tag {
>>    A, B,
>>    version (symbol) {
>>       C, D,
>>    }
>>    E,
>> }
>>
>> but it doesn't work. I know I could do
>>
>> version (symbol) {
>>    enum Tag { A, B, C, D, E }
>> } else {
>>    enum Tag { A, B, E }
>> }
>>
>> but I don't want to do that since in my case, there can be quite a few
>> elements outside of the version, and I don't want to repeat them all.
>
> template Version(char[] ident)
> {
>     mixin(`version(`~ident~`) const Version = true;
>            else const Version = false;`);
> }
>
> char[] genEnum(bool flag1, bool flag2)
> {
>     return "A, B, "
>          ~ (flag1 ? "C, D, " : "")
>          ~ "E, F, "
>          ~ (flag2 ? "G, H, " : "")
>          ~ "G, ";
> }
>
> mixin(genEnum( Version!(`symbol`), Version!(`somethingElse`) ));
>
> ... is about the best you can do, I think.

Hey, thanks for the idea. I worked with it a little bit to remove the  
generator function and use multiline strings. This is what I came up with.

mixin(q"ENUM
enum Tag
{
    A, B,
ENUM"~(Version!("symbol")?q"ENUM
    C, D,
ENUM":"")~q"ENUM
    E,
}
ENUM");

That's not pretty, but it's good enough for me.


More information about the Digitalmars-d-learn mailing list