Why version() ?

grauzone none at example.net
Tue Feb 10 03:48:54 PST 2009


bobef wrote:
> I was thinking... what is the point of version() ? It is so inflexible. There is no even version(!...). Why not "static if(version(DMD))" or static if(is(version == DMD))?
> 
> Regards,
> bobef

One of my pet peeves.

The only advantages of version() over static if() are:
1. You can pass version flags as command line parameters
2. It is shorter.

Some severe disadvantages of version():
1. They can't contain expressions (like "version(linux && mac)").
2. Typos are not catched! "version(linxu)" will just silently compile.

Proposal to solve these problems:

Remove "version()" and replace it by "static if()" (actually, it would 
make sense to turn version into an alias for static if). Introduce const 
declarations, which get their value from commandline:

//command line for dmd to set the constant cLinux

dmd -version cLinux=true

//define a const variable, whose value is taken from the commandline
//they always need to be declared, the compiler shouldn't just
//arbitrarily introduce new symbols into the modules' scope!
//(exact syntax needs to be discussed)

const bool cLinux = $cmdarg;

//instead of version(linux)
//unlike version, arbitrary expressions are allowed

static if(cLinux) {
	//Linux code goes here
}

static if(cLinxu) { //oops, typo, the compiler will loudly complain!

As an additional feature, you could allow passing of additional data 
types instead of only bool. Strings, for example.

Unlike version(), cLinux always needs to be defined on the command line. 
I don't consider this a disadvantage.

I'd also leave debug() as it is.



More information about the Digitalmars-d mailing list