4 ms·
> D has no need for a preprocessor How does this work, specifically conditional compilation? I have lots of code like this: #if __APPLE__ /* do thing
by ridiculous_fish 12y ago
> D has no need for a preprocessor
How does this work, specifically conditional compilation? I have lots of code like this:
#if __APPLE__
/* do things the OS X way */
#else
/* do things the Linux way */
#endif
Where the OS X way calls functions that do not exist on Linux, and vice-versa. How does D handle this?
- vitaut 12y agoIn many cases you don't need preprocessor for this even in C++. For example you can detect the availability of system functions using function overloading as described in http://zverovich.net/2015/03/13/reliable-detection-of-strerror-variants.html http://zverovich.net/2015/03/13/reliable-detection-of-strerr...
- kevinnk 12y agoMaybe someone with more D experience can chime in, but D has conditional compilation (ie "static if") http://dlang.org/version.html http://dlang.org/version.html
- WalterBright 12y agoCheck out https://github.com/D-Programming-Language/phobos/blob/master/std/file.d https://github.com/D-Programming-Language/phobos/blob/master... at line 33 and on. You'll see: version (Windows) { ... } etc. For the specification on how this works: http://dlang.org/version.html http://dlang.org/version.html The salient point is that conditional compilation in D is not text based, it is AST based. Furthermore, for 'static if', the conditional expression has access to the full symbol table and power of the D language - it is not a separate preprocessor language with its own rules and separate symbol table.
- ridiculous_fish 12y agoThank you for the reply. That's very elegant!
- deleted 12y ago[deleted]
- ape4 12y agoCan it do more complex ifdefs - with or's and and's etc?
- WalterBright 12y ago'static if' can - it has full access to the language.
- CyberShadow 12y agoLack of a preprocessor does not imply lack of conditional compilation: http://dlang.org/version.html http://dlang.org/version.html The constructs are simply built into the language. This has considerable benefits: you can use e.g. `static if` to enable/disable code based on conditions which refer to constants declared in the program (there is no longer a separate namespace for preprocessor defines and program constants), and with CTFE, you can use any expression that can be evaluated during compilation.
- Zardoz84 12y agoThe language it's itself his own preprocesor, thanks to CTFE. I think that it is what I love more of D.
- Zardoz84 12y agoversion (Posix) { version(OSX) { // Weird stuff on OSX } // Sane stuff for Linux or FreeBSD } version (Windows) { // Weird stuff for Windows } But think that this is processed by the compiler, not by a text pre-processor. Also, you can use your own version stuff, for example here : https://github.com/Zardoz89/nBodySim/blob/master/source/app.d https://github.com/Zardoz89/nBodySim/blob/master/source/app.... there is a "PFor" version to allow to compile a version of the program that uses a parallel foreach (an N Body simulator)