www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - enum and static if

reply "Namespace" <rswhite4 gmail.com> writes:
This code does not work:

----
enum Test {
     Foo,
static if (__VERSION__ >= 2067)
     Bar,
}
     Quatz
}
----

Any chance that this could work?
Mar 11 2015
parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Wed, 11 Mar 2015 13:48:45 +0000, Namespace wrote:

 This code does not work:
=20
 ----
 enum Test {
      Foo,
 static if (__VERSION__ >=3D 2067)
      Bar,
 }
      Quatz
 }
 ----
=20
 Any chance that this could work?
nope. `static if` is statement, so it works only where statement is=20 allowed. the same is true for `version`. this is by design.=
Mar 11 2015
next sibling parent reply "wobbles" <grogan.colin gmail.com> writes:
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?
Mar 11 2015
parent reply ketmar <ketmar ketmar.no-ip.org> writes:
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:
=20
 ----
 enum Test {
      Foo,
 static if (__VERSION__ >=3D 2067)
      Bar,
 }
      Quatz
 }
 ----
=20
 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.
=20 You can do something like static if (__VERSION__ >=3D 2067) enum Test{ ... } else enum Test{ ... } =20 as a workaround?
sure, but you have to copypaste the whole enum in both places. maybe=20 allowing `version` in enums worth a ER...=
Mar 11 2015
parent reply Artur Skawina via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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
Mar 11 2015
parent reply ketmar <ketmar ketmar.no-ip.org> writes:
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:
=20
 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__ >=3D 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__ >=3D 2067) enum Test{ ... } else enum Test{ ... } as a workaround?
=20 sure, but you have to copypaste the whole enum in both places. maybe allowing `version` in enums worth a ER...
=20 mixin(` enum Test { Foo,` ~(__VERSION__>=3D2067?` Bar,`:``) ~` Quatz }`); =20 artur
yes, it works. it also can be a participant in "ugly D code of the month"=20 contest. ;-)=
Mar 11 2015
parent "Nicolas Sicard" <dransic gmail.com> writes:
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," : "")); :)
Mar 11 2015
prev sibling parent reply "Namespace" <rswhite4 gmail.com> writes:
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.
Thanks, I've hoped that 'static if' is a full replacement for #if
Mar 11 2015
parent Jonathan M Davis via Digitalmars-d-learn writes:
On Wednesday, March 11, 2015 14:55:01 Namespace via Digitalmars-d-learn wrote:
 Thanks, I've hoped that 'static if' is a full replacement for #if
Walter Bright has a rather dim view of #if and friends in C/C++ given how often he's seen them abused, which is why version and static if are more restricted in D - in particular, it's why version won't take a boolean expression. And while some folks get really annoyed about having to duplicate code for different versions, he seems to be of the opinion that that's the right way to do it and that the C/C++ way is incredibly error-prone. Personally, I'd say that this particular use case probably would be fine, but it would complicate how static if works, so I suspect that he'd be against it. - Jonathan M Davis
Mar 11 2015