enum and static if

Nicolas Sicard via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 11 10:38:00 PDT 2015


On Wednesday, 11 March 2015 at 17:19:20 UTC, ketmar wrote:
> On Wed, 11 Mar 2015 18:17:38 +0100, Artur Skawina via 
> Digitalmars-d-learn
> wrote:
>
>> On 03/11/15 15:41, ketmar via Digitalmars-d-learn wrote:
>>> On Wed, 11 Mar 2015 14:36:07 +0000, wobbles wrote:
>>> 
>>>> On Wednesday, 11 March 2015 at 14:34:32 UTC, ketmar wrote:
>>>>> On Wed, 11 Mar 2015 13:48:45 +0000, Namespace wrote:
>>>>>
>>>>>> This code does not work:
>>>>>>
>>>>>> ----
>>>>>> enum Test {
>>>>>>      Foo,
>>>>>> static if (__VERSION__ >= 2067)
>>>>>>      Bar,
>>>>>> }
>>>>>>      Quatz
>>>>>> }
>>>>>> ----
>>>>>>
>>>>>> Any chance that this could work?
>>>>>
>>>>> nope. `static if` is statement, so it works only where 
>>>>> statement is
>>>>> allowed. the same is true for `version`. this is by design.
>>>>
>>>> You can do something like static if (__VERSION__ >= 2067)
>>>>      enum Test{ ... }
>>>> else
>>>>      enum Test{ ... }
>>>>
>>>> as a workaround?
>>> 
>>> sure, but you have to copypaste the whole enum in both 
>>> places. maybe
>>> allowing `version` in enums worth a ER...
>> 
>>    mixin(`
>>    enum Test {
>>        Foo,`
>>    ~(__VERSION__>=2067?`
>>        Bar,`:``)
>>    ~`  Quatz }`);
>> 
>> artur
>
> yes, it works. it also can be a participant in "ugly D code of 
> the month"
> contest. ;-)

The second prize in this contest could for:

mixin(`
     enum Test {
         Foo,
         %s
         Quatz
     }`
.format(__VERSION__ >= 2067 ? "Bar," : ""));

:)




More information about the Digitalmars-d-learn mailing list