www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Testing whether static foreach is available

reply Jean-Louis Leroy <jl leroy.nyc> writes:
Is there a way to test whether 'static foreach' is available, 
e.g. via a version symbol?

Also, is it possible to conditionally compile depending on the 
compiler's version, not only the compiler (I mean, not compile a 
block if it's GDC version 5.4.1 or lower)?

I did look at the docs, several times, couldn't find it...
Sep 09 2017
next sibling parent reply jmh530 <john.michael.hall gmail.com> writes:
On Saturday, 9 September 2017 at 16:30:51 UTC, Jean-Louis Leroy 
wrote:
 Also, is it possible to conditionally compile depending on the 
 compiler's version, not only the compiler (I mean, not compile 
 a block if it's GDC version 5.4.1 or lower)?
What you really need is a version statement based on the version of the D language, not the version of DMD or LDC or GDC per se. Something like version(DLANGSEMVER >= 2.076.0) { //include static foreach code. } but we can't do conditionals in version blocks, so you'd need a static if. Alternately, we'd also need a trait or something to get the semver of the compiler.
Sep 09 2017
next sibling parent Jean-Louis Leroy <jl leroy.nyc> writes:
On Saturday, 9 September 2017 at 16:53:19 UTC, jmh530 wrote:
 On Saturday, 9 September 2017 at 16:30:51 UTC, Jean-Louis Leroy 
 wrote:
 Also, is it possible to conditionally compile depending on the 
 compiler's version, not only the compiler (I mean, not compile 
 a block if it's GDC version 5.4.1 or lower)?
What you really need is a version statement based on the version of the D language, not the version of DMD or LDC or GDC per se. Something like version(DLANGSEMVER >= 2.076.0) { //include static foreach code. } but we can't do conditionals in version blocks, so you'd need a static if. Alternately, we'd also need a trait or something to get the semver of the compiler.
For some of my needs (like here https://github.com/jll63/openmethods.d/blob/v1.0.0-rc.2/source/ penmethods.d#L1509) 'static if' would work. But I'd really like to provide a template mixin alternative to the string mixin I use in openmethods ('mixin(registerMethods)'). Hmmm...Generally speaking, the lack of [your suggestion] will slow down the adoption of new cool features like 'static foreach'.
Sep 09 2017
prev sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Saturday, 9 September 2017 at 16:53:19 UTC, jmh530 wrote:

 version(DLANGSEMVER >= 2.076.0)
 {
    //include static foreach code.
 }

 but we can't do conditionals in version blocks, so you'd need a 
 static if. Alternately, we'd also need a trait or something to 
 get the semver of the compiler.
Actually: static if(__VERSION__ >= 2076) Better to use this than the compiler version, as it specifies the version of the front end, so when LDC and GDC catch up it will work for those as well. But Adam's point about the false branch still holds, so: static if(__VERSION__ >= 2076) mixin(`static foreach(i; 0..3) writeln(i);`); else writeln("Version too low"); The same is true for version statements.
Sep 09 2017
next sibling parent reply jmh530 <john.michael.hall gmail.com> writes:
On Sunday, 10 September 2017 at 01:49:42 UTC, Mike Parker wrote:
 But Adam's point about the false branch still holds, so:

 static if(__VERSION__ >= 2076)
     mixin(`static foreach(i; 0..3) writeln(i);`);
 else
     writeln("Version too low");

 The same is true for version statements.
But it would work for changes to phobos, right? So there's a silver lining ;)
Sep 09 2017
parent reply Petar Kirov [ZombineDev] <petar.p.kirov gmail.com> writes:
On Sunday, 10 September 2017 at 02:13:58 UTC, jmh530 wrote:
 On Sunday, 10 September 2017 at 01:49:42 UTC, Mike Parker wrote:
 But Adam's point about the false branch still holds, so:

 static if(__VERSION__ >= 2076)
     mixin(`static foreach(i; 0..3) writeln(i);`);
 else
     writeln("Version too low");

 The same is true for version statements.
But it would work for changes to phobos, right? So there's a silver lining ;)
Since Phobos always uses the latest compiler frontend, there's nothing to check ;)
Sep 10 2017
parent jmh530 <john.michael.hall gmail.com> writes:
On Sunday, 10 September 2017 at 10:43:24 UTC, Petar Kirov 
[ZombineDev] wrote:
 Since Phobos always uses the latest compiler frontend, there's 
 nothing to check ;)
Here's an example of what I mean. In D 2.074.0, std.format added some compile-time format string functionality. So if I compile with any DMD since then, I'm good to go. However, if I compile with LDC 1.3.0 or earlier (or an older DMD or GDC), then Phobos is at 2.073.2 (or earlier) and any unittests using this compile-time format will fail. So I was thinking I could put in a test for >= 2074 and put any code using the compile-time format string stuff in there. I haven't tried it yet, but I was thinking that this would work rather than the compiles approach because I wouldn't think there is an issue with parsing the format compiles code. The issue is whether it is in phobos or not.
Sep 10 2017
prev sibling parent Moritz Maxeiner <moritz ucworks.org> writes:
On Sunday, 10 September 2017 at 01:49:42 UTC, Mike Parker wrote:
 On Saturday, 9 September 2017 at 16:53:19 UTC, jmh530 wrote:

 version(DLANGSEMVER >= 2.076.0)
 {
    //include static foreach code.
 }

 but we can't do conditionals in version blocks, so you'd need 
 a static if. Alternately, we'd also need a trait or something 
 to get the semver of the compiler.
Actually: static if(__VERSION__ >= 2076) Better to use this than the compiler version, as it specifies the version of the front end, so when LDC and GDC catch up it will work for those as well.
If __VERSION__ is *guaranteed* to be the language frontend version (and testing shows it is in dmd, ldc, and gdc, at least), shouldn't the spec [1] state that (right now it's only required to be some generic "compiler version")? [1] https://dlang.org/spec/lex.html#specialtokens
Sep 10 2017
prev sibling next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 9 September 2017 at 16:30:51 UTC, Jean-Louis Leroy 
wrote:
 I did look at the docs, several times, couldn't find it...
you should use my unofficial docs http://dpldocs.info/version std.compiler looks useful http://dpldocs.info/experimental-docs/std.compiler.version_minor.html static foreach was introduced in 2.076 so import std.compiler; static if(version_minor >= 76) { // it is available } BUT, this won't do what I suspect you want... if you put a static foreach in there and compile with an old compiler... it will still cause a parse error. The false branch of a static if still need to parse correctly, and static foreach won't in old D. You can work around that by perhaps importing alternative modules or using string mixin that contains the new code (similar techniques for libs that are both D1 and D2 compatible)
Sep 09 2017
parent jmh530 <john.michael.hall gmail.com> writes:
On Saturday, 9 September 2017 at 17:32:54 UTC, Adam D. Ruppe 
wrote:
 std.compiler looks useful
 http://dpldocs.info/experimental-docs/std.compiler.version_minor.html
Did not realize that was in there. TIL.
Sep 09 2017
prev sibling parent Petar Kirov [ZombineDev] <petar.p.kirov gmail.com> writes:
On Saturday, 9 September 2017 at 16:30:51 UTC, Jean-Louis Leroy 
wrote:
 Is there a way to test whether 'static foreach' is available, 
 e.g. via a version symbol?

 Also, is it possible to conditionally compile depending on the 
 compiler's version, not only the compiler (I mean, not compile 
 a block if it's GDC version 5.4.1 or lower)?

 I did look at the docs, several times, couldn't find it...
I would advice against checking the compiler version (as this is brittle) and instead check if the particular feature is really available. enum hasFeatureAvailable(string code) = __traits(compiles, { mixin (code); }); enum hasStaticForeach = hasFeatureAvailable!("static foreach(i; 0..1) {}"); pragma (msg, "Supports `static foreach`: ", hasStaticForeach);
Sep 10 2017