compilers w/ different language features: version block

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sun Feb 25 00:52:37 UTC 2018


On Sunday, February 25, 2018 00:36:16 kdevel via Digitalmars-d-learn wrote:
> A code fragment using static foreach
>
> https://forum.dlang.org/thread/jiefcxwqbjzqnmtazkwy@forum.dlang.org#post-b
> eruryblsptnunsowjph:40forum.dlang.org
>
> does not compile with the current GDC (GCC 4.9.4 and 5.5.0). I
> tried to encapsulate this code into a version block but GDC still
> checks the syntax. Is there any more elegant way besides of using
> a mixin?:
>
>     version (DigitalMars) {
>        pragma (msg, "DMD");
>        mixin (`
>        enum typenames = ["float", "double", "real", "short",
> "int", "long"];
>        static foreach (s; typenames) {
>           pragma (msg, s);
>           disp[s] = mixin ("&mymain!" ~ s);
>        }
>        `);
>     }

For static foreach? No. As I undarstand it, because of how the grammar works
with static foreach, there is no way to have it in the code (other than a
string mixin) unless the compiler supports it.

If you want to use newer features, I would suggest avoiding gdc until they
finish playing catch-up. They have yet to do an official release that's more
up-to-date than 2.068.2 of the front-end. When the frontend switched from
C++ to D, the gdc folks did a lot of rewriting to update their stuff, and
it's taken them a while to finish. What they have on github should be more
up-to-date, but I don't know what state it's in, and they have yet to do a
release with all of those updates. At some point, the gdc guys will catch
up, but for now, it's probably better to avoid it - especially if you want
to use newer features like static foreach.

So, I would suggest that you either use dmd or ldc. ldc has actually managed
to stay fairly up-to-date and is usually at most a release or so behind dmd.

But if you really want code to work with both gdc and and newer compilers,
then as far as static foreach goes, you'll have to do stuff like use string
mixins or different files for different compilers. Unfortunately, you can't
just use different version blocks.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list