Set variable at compile time

Adam D. Ruppe destructionator at gmail.com
Wed Oct 30 13:19:09 PDT 2013


It won't really work on the command line alone, but the way I do 
it is a two step thing. First, make the thing use a config module:

import myproject.config;

alias Thing Thing_Impl!dim;

then you go ahead and use Thing, which is instantiated with the 
dimension. Then the user makes a file:

module myproject.config;

enum dim = 10; // or whatever


and compiles it all:

dmd main.d yourlib.d config.d

and it works out. They can swap out config.d for other files with 
other values and definitions on the command line to customize it.


It would also be possible to provide a default if you go with the 
whole library route, compiling the default into the .lib and 
giving the default in the project include path, both of which 
would be overridden by the user's explicit config source file on 
the command line.



So not as simple as -Ddim=10, but gets the job done. You can also 
use this technique with static if to version stuff out they don't 
want and other similar tasks; I actually like it better than 
-version for the most part.


More information about the Digitalmars-d-learn mailing list