Version statement

Nick Sabalausky a at a.a
Tue Dec 14 11:38:44 PST 2010


"Jacob Carlborg" <doob at me.com> wrote in message 
news:ie8fdf$ol8$1 at digitalmars.com...
> On 2010-12-14 19:46, Extrawurst wrote:
>> Hi i just want to discuss two points about D version statements.
>>
>> 1) Why is it not possible to negate the condition of a version
>> statement. I think it is unintuitive and keeps me writing weird
>> statements like:
>> version(Win32){}else{version = NotWin32;}
>
> That has been suggested many times before, If I recall correctly Walter 
> doesn't like it. You could use bool constants and static if instead to get 
> the behavior you want. It's ugly but it works, here you have most of the 
> predefined version statements as bool constants: 
> http://dsource.org/projects/dstep/browser/dstep/internal/Version.d
>

Not that I agree or disagree with this, but IIRC, the reason Walter gave as 
to why he's against it is that he feels strongly that every platform you 
intend to support should be used explicitly so that other platforms don't 
appear to work by accident but then have bugs because something needs to be 
done differently.

Ex: Suppose there are OSes: OS_A, OS_B, and OS_C, and you have code like 
this:

version(OS_A)
{
    void foo() { /* Code for OS_A */ }
}
else
{
    void foo() { /* Code for OS_B and OS_C */ }
}

But now, when you or someone else comes along and compiles it for OS_D, it's 
going to silently use the code for OS_B and OS_C *regardless* of whether or 
not that's correct for OS_D. Maybe OS_D needs to use the code for OS_A. Or 
maybe it needs to use something completely different.

But if you write it like this:

version(OS_A)
{
    void foo() { /* Code for OS_A */ }
}

version(OS_B) version = UseAlternateFoo;
version(OS_C) version = UseAlternateFoo;
version(UseAlternateFoo)
{
    void foo() { /* Code for OS_B and OS_C */ }
}

Now, if you or someone else compiles for OS_D, you'll get an error that 
"foo()" is undefined (at least if "foo()" is actually used anywhere). This 
forces you to actually think about which foo() OS_D should use or if it 
should use an entitrely different one, instead of just randomly choosing 
one. So this is safer.

Walter feels that disallowing "version( ! BLAH )" discourages the 
error-prone first style and encourages the safer second-style. Not everyone 
agrees though. I'm not quite sure how I feel about it.

>> 2) I would really like the idea to be able to get a compiletime string
>> of version identifiers in some kind of way. Perhaps even an array of
>> version-strings.
>>
>> Any thoughts on that ?
>>

Nothing that I know of, but there's been a lot of discussion about 
improvements that the version() system really, really needs. Unfortunately 
doesn't seem to be a high priority though.




More information about the Digitalmars-d mailing list