www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Multiversion conditional compilation

reply Diego Lago <Diego_member pathlink.com> writes:
Hello, I am testing D compiler with various things (in my spare time) and I was
programming conditional compilation and I have found one thing that is not into
the language: this is multiversion conditional compilation. See next source:

//-------------------source--------------------
import std.stdio; 

alias char[] string;

int
main(string[] args)
{
version(win) {
printf("Windows\n");
}
version(lin) {
printf("Linux"\n);
}
version(mac) {
printf("Mac\n");
}
/*
version(win,lin) {
printf("Windows & Linux\n");
}
version(lin,mac) {
printf("Linux & Mac\n");
}
*/
printf("All versions.\n");
return 0;
}
//-----------------end source---------------------

This portion of code compile witout problems but if I want to compile some piece
of code for more than one version (commented source), I have to write it twice
and, in the manner I show here, it can be done with less code. Could it be
included into the language in next versions?

Thanks for your attention, regards.

--
Diego Lago
beosman gmail.com

P.D.: Sorry, I can't speak English very well, so it can be ununderstandable.
Jan 11 2006
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Diego Lago wrote:

 This portion of code compile witout problems but if I want to compile some
piece
 of code for more than one version (commented source), I have to write it twice
 and, in the manner I show here, it can be done with less code. Could it be
 included into the language in next versions?
Walter doesn't want expressions in "version", so you need workarounds... version(win) version(lin) version = win_and_lin; version(win_and_lin) { printf("Windows & Linux\n"); } Just as an example of course, as the real ones are "Windows" and "linux" --anders
Jan 11 2006
next sibling parent reply Chad J <gamerchad2.no-ip.org/email.txt LeftOfAtSignForEmail.meh> writes:
Anders F Björklund wrote:

 
 Walter doesn't want expressions in "version", so you need workarounds...
 
Say it ain't so! I would love to be able to use, at the very least, boolean operators inside of version. So like: version(D_InlineAsm_X86 && X86) I ran into this last night and ended up with version(D_InlineAsm_X86) { version(X86) { ... // cpu feature detection code here }} It works I suppose, but it sucks when you might end up having to nest 10 versions into each other! I'd much rather do something like version( D_InlineAsm_X86 && X86 && ... && ...) { ... } I also tried to do this: version(D_InlineAsm_X86) { version(X86) { version = UseX86Asm; } } but it gives me the following compiler errors (at that line): (condition) expected following version found '=' instead of statement I thought the spec allowed for that, and it seems to be the same kinda thing Anders just posted. I am using compiler version 0.142 (the latest AFAIK). So why the compiler errors?
Jan 11 2006
next sibling parent reply J C Calvarese <technocrat7 gmail.com> writes:
In article <dq4m1t$2per$1 digitaldaemon.com>, Chad J says...
Anders F Björklund wrote:

 
 Walter doesn't want expressions in "version", so you need workarounds...
 
Say it ain't so!
I'm afraid Anders is right. Many of us (myself included) have been asking for cooler version statements for years now. I've long since given up trying to get him to change his mind. :(
I would love to be able to use, at the very least, boolean operators 
inside of version.  So like:  version(D_InlineAsm_X86 && X86)

I ran into this last night and ended up with

version(D_InlineAsm_X86) { version(X86)
{
	... // cpu feature detection code here
}}

It works I suppose, but it sucks when you might end up having to nest 10 
versions into each other!
I'd much rather do something like
version(   D_InlineAsm_X86
	&& X86
	&& ...
	&& ...)
{
	...
}

I also tried to do this:
version(D_InlineAsm_X86) { version(X86) { version = UseX86Asm; } }
That line works for me (at least it compiles). Here's a whole "program". It's trivial, but it does compiles: version(D_InlineAsm_X86) { version(X86) { version = UseX86Asm; } } void main() {}
but it gives me the following compiler errors (at that line):
(condition) expected following version
found '=' instead of statement
I suspect something else is generating that error. Sometimes the error messages seems to point to a particular line as the culprit, but the problem is really right after or right before. It's a good compiler, but it can't read minds. ;)
I thought the spec allowed for that, and it seems to be the same kinda 
thing Anders just posted.  I am using compiler version 0.142 (the latest 
AFAIK). So why the compiler errors?
Well, actually, the latest compiler is 0.143 (just released a few hours ago), and that's what I tested it with it. So I guess you could try upgrading. But I suspect you just have a pesky extra semi-colon or an extra brace in a troublesome place. Good luck. I wish version could do more in the way you suggested, but I think the clever tricks can get us pretty close. jcc7
Jan 11 2006
parent reply Chad J <gamerchad2.no-ip.org/email.txt LeftOfAtSignForEmail.meh> writes:
J C Calvarese wrote:
I also tried to do this:
version(D_InlineAsm_X86) { version(X86) { version = UseX86Asm; } }
That line works for me (at least it compiles). Here's a whole "program". It's trivial, but it does compiles: version(D_InlineAsm_X86) { version(X86) { version = UseX86Asm; } } void main() {}
but it gives me the following compiler errors (at that line):
(condition) expected following version
found '=' instead of statement
I suspect something else is generating that error. Sometimes the error messages seems to point to a particular line as the culprit, but the problem is really right after or right before. It's a good compiler, but it can't read minds. ;)
That line I wrote was in a static this() {} function, at the module scope. I moved it out of static this(), and it compiled just fine. So I think I figured out why it wasn't working - version = something; doesn't work within function bodies. try this: void main() { version = something; } Should fail to compile, same errors as previous post. I couldn't find this in the specs, is this a bug?
Jan 11 2006
parent Hasan Aljudy <hasan.aljudy gmail.com> writes:
Chad J wrote:
 J C Calvarese wrote:
 
 I also tried to do this:
 version(D_InlineAsm_X86) { version(X86) { version = UseX86Asm; } }
That line works for me (at least it compiles). Here's a whole "program". It's trivial, but it does compiles: version(D_InlineAsm_X86) { version(X86) { version = UseX86Asm; } } void main() {}
 but it gives me the following compiler errors (at that line):
 (condition) expected following version
 found '=' instead of statement
I suspect something else is generating that error. Sometimes the error messages seems to point to a particular line as the culprit, but the problem is really right after or right before. It's a good compiler, but it can't read minds. ;)
That line I wrote was in a static this() {} function, at the module scope. I moved it out of static this(), and it compiled just fine. So I think I figured out why it wasn't working - version = something; doesn't work within function bodies. try this: void main() { version = something; } Should fail to compile, same errors as previous post. I couldn't find this in the specs, is this a bug?
Maybe the spec doesn't say it directly, but, version = something is called "VersionSpecification" and it's only allowed as a DeclDef; you can't have a DeclDef inside a FunctionBody.
Jan 11 2006
prev sibling parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Chad J wrote:

 Walter doesn't want expressions in "version", so you need workarounds...
Say it ain't so!
Or at least: "it hasn't been deemed important enough to include yet"...
 I would love to be able to use, at the very least, boolean operators 
 inside of version.  So like:  version(D_InlineAsm_X86 && X86)
 
 I ran into this last night and ended up with
 
 version(D_InlineAsm_X86) { version(X86)
 {
     ... // cpu feature detection code here
 }}
Not that it affects the issue, but for *this* particular example the version(X86) is not needed. As Walter has already included that ? The new "D_InlineAsm_X86" is the same as the corresponding old code: version(X86) version (D_InlineAsm) { version = D_InlineAsm_X86; } There is no support for inline PPC asm in GDC yet (but there is in GCC), but I don't like adding certain arch to the language define like that... It makes sense in practice, but I would rather have both of them around. version (D_InlineAsm) { version(X86) version = D_InlineAsm_X86; version(PPC) version = D_InlineAsm_PPC; } --anders PS. Then again, "version(PPC)" is still missing from the list of versions ? (http://www.digitalmars.com/d/version.html) Along with: "version(Unix)" Both have been in "version(GNU)", i.e. GDC, for a rather long time now.
Jan 12 2006
prev sibling parent reply Carlos Santander <csantander619 gmail.com> writes:
Anders F Björklund escribió:
 Diego Lago wrote:
 
 This portion of code compile witout problems but if I want to compile 
 some piece
 of code for more than one version (commented source), I have to write 
 it twice
 and, in the manner I show here, it can be done with less code. Could 
 it be
 included into the language in next versions?
Walter doesn't want expressions in "version", so you need workarounds... version(win) version(lin) version = win_and_lin; version(win_and_lin) { printf("Windows & Linux\n"); } Just as an example of course, as the real ones are "Windows" and "linux" --anders
Shouldn't it be: version(win) version = win_or_lin; version(lin) version = win_or_lin; version(win_or_lin) { printf("Windows & Linux\n"); } ? Otherwise, it doesn't make sense... -- Carlos Santander Bernal
Jan 12 2006
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Carlos Santander wrote:

 Shouldn't it be:
 
 version(win)
     version = win_or_lin;
 
 version(lin)
     version = win_or_lin;
 
 version(win_or_lin) {
   printf("Windows & Linux\n");
 }
 
 ? Otherwise, it doesn't make sense...
It was just an example, by the OP. --anders PS. In your "corrected" example, printf("Windows | Linux\n");
Jan 12 2006
parent reply Carlos Santander <csantander619 gmail.com> writes:
Anders F Björklund escribió:
 Carlos Santander wrote:
 
 Shouldn't it be:

 version(win)
     version = win_or_lin;

 version(lin)
     version = win_or_lin;

 version(win_or_lin) {
   printf("Windows & Linux\n");
 }

 ? Otherwise, it doesn't make sense...
It was just an example, by the OP. --anders PS. In your "corrected" example, printf("Windows | Linux\n");
I know both things, but it just didn't make sense. It's like boolean operators mean different things in programming languages than in natural languages: "women and children first", "do you want chicken or fish?". I understood that printf as saying "this is common for Windows and Linux". Anyway, when the OP actually codes, he'll see what he really wanted. -- Carlos Santander Bernal
Jan 12 2006
next sibling parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Carlos Santander wrote:

 It's like boolean operators mean different things in programming 
 languages than in natural languages: "women and children first", "do you 
 want chicken or fish?". I understood that printf as saying "this is 
 common for Windows and Linux".
Correct, it turned out it was my mistake and not the original poster.
 version(win,lin) {
 printf("Windows & Linux\n");
Just like you said, it should be: version(win) version = win_or_lin; version(lin) version = win_or_lin; version(win_or_lin) { printf("Windows & Linux\n"); } Also, and even funnier, is that "win" or "lin" aren't right either. As per Walter's spelling prefs, it should be "Windows" and "linux" --anders
Jan 12 2006
prev sibling next sibling parent Bruno Medeiros <daiphoenixNO SPAMlycos.com> writes:
Carlos Santander wrote:
 Anders F Björklund escribió:
 
 Carlos Santander wrote:

 Shouldn't it be:

 version(win)
     version = win_or_lin;

 version(lin)
     version = win_or_lin;

 version(win_or_lin) {
   printf("Windows & Linux\n");
 }

 ? Otherwise, it doesn't make sense...
It was just an example, by the OP. --anders PS. In your "corrected" example, printf("Windows | Linux\n");
I know both things, but it just didn't make sense. It's like boolean operators mean different things in programming languages than in natural languages: "women and children first", "do you want chicken or fish?". I understood that printf as saying "this is common for Windows and Linux".
In natural language, in some cases they are not even boolean operators. For instance, 'or' when used in interrogatives has a "choice" meaning instead of a boolean meaning. Consider: "Is this blue and red?" Answers: 'Yes' or 'No' "Is this blue or red?" Answers: 'blue' or 'red' And 'and' has more of a meaning of "addition, aggregation", which is more general than booleanness, altough it still works like that. "Do this and that" Meaning: "Do this and do that", more of aggregation like, since imperatives don't even have truth values. -- Bruno Medeiros - CS/E student "Certain aspects of D are a pathway to many abilities some consider to be... unnatural."
Jan 13 2006
prev sibling parent S. Chancellor <dnewsgr mephit.kicks-ass.org> writes:
On 2006-01-12 16:24:43 -0800, Carlos Santander <csantander619 gmail.com> said:

 Anders F Björklund escribió:
 Carlos Santander wrote:
 
 Shouldn't it be:
 
 version(win)
     version = win_or_lin;
 
 version(lin)
     version = win_or_lin;
 
 version(win_or_lin) {
   printf("Windows & Linux\n");
 }
 
 ? Otherwise, it doesn't make sense...
It was just an example, by the OP. --anders PS. In your "corrected" example, printf("Windows | Linux\n");
I know both things, but it just didn't make sense. It's like boolean operators mean different things in programming languages than in natural languages: "women and children first", "do you want chicken or fish?". I understood that printf as saying "this is common for Windows and Linux". Anyway, when the OP actually codes, he'll see what he really wanted.
Sounds like you haven't been programming for long enough :) Eventually you'll just answer True to those questions. -S.
Jan 21 2006