digitalmars.D.learn - compilers w/ different language features: version block
- kdevel (17/17) Feb 24 2018 A code fragment using static foreach
- Seb (14/32) Feb 24 2018 Are you looking for something like this?
- Seb (3/19) Feb 24 2018 Ah sorry I replied too fast. Yeah `mixin` is the only way to
- Seb (24/51) Feb 24 2018 BTW there's one trick that might work on your setup - conditional
- Jonathan M Davis (20/38) Feb 24 2018 For static foreach? No. As I undarstand it, because of how the grammar w...
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); } `); }
Feb 24 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/PZN5NvIs there any more elegant way besides of using a mixin?:Don't support GDC (or only GDC-master)?
Feb 24 2018
On Sunday, 25 February 2018 at 00:42:20 UTC, Seb wrote:On Sunday, 25 February 2018 at 00:36:16 UTC, kdevel wrote:Ah sorry I replied too fast. Yeah `mixin` is the only way to workaround this if you really, really have to support GDC.[...]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[...]Don't support GDC (or only GDC-master)?
Feb 24 2018
On Sunday, 25 February 2018 at 01:19:02 UTC, Seb wrote:On Sunday, 25 February 2018 at 00:42:20 UTC, Seb wrote:BTW there's one trick that might work on your setup - conditional imports: --- static if (__traits(compiles, () { static foreach (i; [0]){} })) version = supportsStaticForeach; void main() { version(supportsStaticForeach) { import compat.staticforeach; } else { import compat.no_staticforeach; } } --- With rdmd this should work out of the box - with DUB you would need to tweak the source paths. DUB allows all options to be compiler-specific, so e.g. something like this for a dub.sdl could work: --- sourcePaths-dmd "compat/staticforeach" sourcePaths-ldc "compat/staticforeach" sourcePaths-gdc "compat/no_staticforeach" ---On Sunday, 25 February 2018 at 00:36:16 UTC, kdevel wrote:Ah sorry I replied too fast. Yeah `mixin` is the only way to workaround this if you really, really have to support GDC.[...]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[...]Don't support GDC (or only GDC-master)?
Feb 24 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
Feb 24 2018