www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Compile time definitions

reply "Brenton" <brenton.n.rayner gmail.com> writes:
Is there a way to define a compile time constant/enum with dmd?  
For example, inserting the svn revision number into my code?  In 
C...

#include <stdio.h>
#ifndef SOMETHING
#define SOMETHING "bar"
#endif
int main() {
	printf("hello world: " SOMETHING " \n");
	return 0;
}


 gcc main.c && ./a.out
hello world: bar
 gcc -DSOMETHING=\"foo\" main.c && ./a.out
hello world: foo How would you recommend I do something like this with D?
Jul 05 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Brenton:

 How would you recommend I do something like this with D?
In D compile-time constants are introduced using the "enum" keyword. You can also use the "-version=..." compiler switch to compile your D code according to some version, that can be a number or identifier. In D there isn't the "-D" switch of gcc, so you can do something similar putting your string into a little textual file, and importing it inside the module using mixin(import("myfilename.txt")) statement plus the -Imypathname compiler switch. Bye, bearophile
Jul 05 2014
parent "Brenton" <brenton.n.rayner gmail.com> writes:
On Saturday, 5 July 2014 at 22:08:52 UTC, bearophile wrote:
 Brenton:

 How would you recommend I do something like this with D?
In D compile-time constants are introduced using the "enum" keyword. You can also use the "-version=..." compiler switch to compile your D code according to some version, that can be a number or identifier. In D there isn't the "-D" switch of gcc, so you can do something similar putting your string into a little textual file, and importing it inside the module using mixin(import("myfilename.txt")) statement plus the -Imypathname compiler switch. Bye, bearophile
Thanks bearophile, using mixin(import("myfilename.txt")) works great.
Jul 05 2014