Jump to page: 1 2
Thread overview
Testing whether static foreach is available
Sep 09
jmh530
Sep 10
jmh530
Sep 10
jmh530
Sep 09
jmh530
September 09
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...

September 09
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.

September 09
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/openmethods.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'.
September 09
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)
September 09
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.
September 10
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.

September 10
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 ;)
September 10
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);


September 10
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 ;)
September 10
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

« First   ‹ Prev
1 2