compilers w/ different language features: version block

Seb seb at wilzba.ch
Sun Feb 25 00:42:20 UTC 2018


On Sunday, 25 February 2018 at 00:36:16 UTC, kdevel wrote:
> A code fragment using static foreach
>
> https://forum.dlang.org/thread/jiefcxwqbjzqnmtazkwy@forum.dlang.org#post-beruryblsptnunsowjph: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);
>       }
>       `);
>    }

Are you looking for something like this?

---
static if (__traits(compiles, () { static foreach (i; [0]){} }))
     version = supportsStaticForeach;

void main()
{
     version(supportsStaticForeach) {
        pragma(msg, "Yay, your compiler is new enough.");
     }
}
---


https://run.dlang.io/is/PZN5Nv

> Is there any more elegant way besides of using a mixin?:

Don't support GDC (or only GDC-master)?


More information about the Digitalmars-d-learn mailing list