www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Version statement

reply Extrawurst <spam extrawurst.org> writes:
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;}

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 ?

Regards,
Stephan
Dec 14 2010
next sibling parent reply Jacob Carlborg <doob me.com> writes:
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
 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 ?

 Regards,
 Stephan
-- /Jacob Carlborg
Dec 14 2010
parent reply "Nick Sabalausky" <a a.a> writes:
"Jacob Carlborg" <doob me.com> wrote in message 
news:ie8fdf$ol8$1 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.
Dec 14 2010
parent reply Walter Bright <newshound2 digitalmars.com> writes:
Nick Sabalausky wrote:
 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.
That particular problem has bitten me probably hundreds of times, and every other programmer as well. And it happens for lots of things besides OS versions. Though a programmer can always defeat the intent. At one point, someone added "support" for another OS in Phobos by copy/pasting the support declarations from another OS into the version block. Naturally, it was all wrong. The problem was I didn't know which parts were copy/pasted, so I had to go through every single declaration one at a time.
Dec 14 2010
next sibling parent "Nick Sabalausky" <a a.a> writes:
"Walter Bright" <newshound2 digitalmars.com> wrote in message 
news:ie8j0d$1a95$1 digitalmars.com...
 Nick Sabalausky wrote:
 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.
That particular problem has bitten me probably hundreds of times, and every other programmer as well. And it happens for lots of things besides OS versions.
I can believe that.
 Though a programmer can always defeat the intent. At one point, someone 
 added "support" for another OS in Phobos by copy/pasting the support 
 declarations from another OS into the version block. Naturally, it was all 
 wrong. The problem was I didn't know which parts were copy/pasted, so I 
 had to go through every single declaration one at a time.
A good comparison utility like Beyond Compare or Ultra Compare would have made that easy. I used Beyond Compare for the first time nearly ten years ago, and from that moment until this day it's one of those things, like a decent text editer, that I just couldn't do without.
Dec 14 2010
prev sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 12/14/10 2:09 PM, Walter Bright wrote:
 Nick Sabalausky wrote:
 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.
That particular problem has bitten me probably hundreds of times, and every other programmer as well. And it happens for lots of things besides OS versions. Though a programmer can always defeat the intent. At one point, someone added "support" for another OS in Phobos by copy/pasting the support declarations from another OS into the version block. Naturally, it was all wrong. The problem was I didn't know which parts were copy/pasted, so I had to go through every single declaration one at a time.
svn diff and svn blame for the rescue :o). Andrei
Dec 14 2010
prev sibling next sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
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;}
The idea is that versions should be positive statements, not some complex formulation from other versions. The latter is commonplace in C, and in my experience nearly always wrong. For example, NotWin32 is, what? It's a bug waiting to happen. The preferred way is: version (Win32) ... else version (linux) ... else static assert(0, "unsupported system");
 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.
I don't know what that means.
Dec 14 2010
parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Walter Bright <newshound2 digitalmars.com> wrote:


 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.
I don't know what that means.
Likely something like __traits( versions ), that returns ["DigitalMars", "Windows", "X86", "LittleEndian", "D_InlineAsm_X86", "D_Version2", "all"]. -- Simen
Dec 14 2010
parent reply Extrawurst <spam extrawurst.org> writes:
On 14.12.2010 20:23, Simen kjaeraas wrote:
 Walter Bright <newshound2 digitalmars.com> wrote:


 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.
I don't know what that means.
Likely something like __traits( versions ), that returns ["DigitalMars", "Windows", "X86", "LittleEndian", "D_InlineAsm_X86", "D_Version2", "all"].
Yes, exactly that.
Dec 14 2010
parent Walter Bright <newshound2 digitalmars.com> writes:
Extrawurst wrote:
 On 14.12.2010 20:23, Simen kjaeraas wrote:
 Walter Bright <newshound2 digitalmars.com> wrote:


 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.
I don't know what that means.
Likely something like __traits( versions ), that returns ["DigitalMars", "Windows", "X86", "LittleEndian", "D_InlineAsm_X86", "D_Version2", "all"].
Yes, exactly that.
I suggest filing this as an enhancement request to bugzilla.
Dec 14 2010
prev sibling parent "Denis Koroskin" <2korden gmail.com> writes:
On Tue, 14 Dec 2010 21:46:47 +0300, Extrawurst <spam extrawurst.org> 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;}

 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 ?

 Regards,
 Stephan
Try using static if instead: version (Win32) { enum IsWin32 = true; } else { enum IsWin32 = false; } static if (IsWin32) { // ... } static if (!IsWin32) { // ... }
Dec 14 2010