Why version() ?

Nick Sabalausky a at a.a
Tue Feb 10 11:02:31 PST 2009


"grauzone" <none at example.net> wrote in message 
news:gmrpj5$1hea$1 at digitalmars.com...
> 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.


I wholeheartedly agree with this 100%. I'd also have to agree with Denis's 
point about the semantic/syntactic restrictions:

"Denis Koroskin" <2korden at gmail.com> wrote in message 
news:op.uo433jipo7cclz at proton.creatstudio.intranet...
> My biggest complaint is that code inside version () {} block should be 
> semantically correct. It creates many obstacles and disallows good 
> use-cases:
> - you cannot mix D1, D2 and (in future) D3 in a single source code file 
> without helf of mixins.
> - you cannot mix asm for different platforms in one source code file 
> (because DMD doesn't understand ARM assembly, for instance)
> - etc





More information about the Digitalmars-d mailing list