www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - passing all writefln arguments

reply "Saaa" <empty needmail.com> writes:
I'm trying to make my own little debug_writefln which only prints when I set 
my DEBUGG to true.

void dWritefln(...){
   if(DEBUGG==true) writefln( ? );
}

What should I place there to make this work, or should I approuch this 
wholly differently? 
Mar 19 2008
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Saaa wrote:
 I'm trying to make my own little debug_writefln which only prints when I set 
 my DEBUGG to true.
 
 void dWritefln(...){
    if(DEBUGG==true) writefln( ? );
 }
 
 What should I place there to make this work, or should I approuch this 
 wholly differently? 
 
 

Easier if you turn it inside out. debug(DEBUGG) { alias writefln dWritefln; } else { void dWritefln(...) {} } --bb
Mar 19 2008
parent reply "Saaa" <empty needmail.com> writes:
Thanks,

As I think that changing a variable is easier than changing the compiler 
line I went with:

const bool DEBUGG=false;

static if (DEBUGG == true) {
 alias writefln dWritefln;
}
else {
 void dWritefln(...) {}
}

Will dWritefln(..){} compile to 'nothing'?
I mean, will calling dWritefln when DEBUGG=false take no cpu tick (does the 
compiler see that it does nothing)?


 Easier if you turn it inside out.

 debug(DEBUGG) {
    alias writefln dWritefln;
 }
 else {
    void dWritefln(...) {}
 }

 --bb 

Mar 20 2008
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Saaa wrote:
 Thanks,
 
 As I think that changing a variable is easier than changing the compiler 
 line I went with:
 
 const bool DEBUGG=false;
 
 static if (DEBUGG == true) {
  alias writefln dWritefln;
 }
 else {
  void dWritefln(...) {}
 }

I think you can set debug versions inside a file too. That way you can have the best of both worlds. Turn it on from the command line, or by modifying the file. debug = DEBUGG; debug(DEBUGG) { alias writefln dWritefln; } else { void dWritefln(...) {} } ... at least that works with 'version'. Not sure if it works with debug too. (If it doesn't I'll file a bug about the inconsistency with 'version'.)
 Will dWritefln(..){} compile to 'nothing'?
 I mean, will calling dWritefln when DEBUGG=false take no cpu tick 

 compiler see that it does nothing)?

Yes. See thread entitled "debug()" over in digitalmars.D. digitalmars.com digitalmars.D:66826 digitalmars.com digitalmars.D:66827 But that may only be with -inline. Not sure what flags Jarrett used when he ran the test. --bb
Mar 20 2008
parent "Saaa" <empty needmail.com> writes:
Seems to work, thanks.


 Saaa wrote:
 Thanks,

 As I think that changing a variable is easier than changing the compiler 
 line I went with:

 const bool DEBUGG=false;

 static if (DEBUGG == true) {
  alias writefln dWritefln;
 }
 else {
  void dWritefln(...) {}
 }

I think you can set debug versions inside a file too. That way you can have the best of both worlds. Turn it on from the command line, or by modifying the file. debug = DEBUGG; debug(DEBUGG) { alias writefln dWritefln; } else { void dWritefln(...) {} } ... at least that works with 'version'. Not sure if it works with debug too. (If it doesn't I'll file a bug about the inconsistency with 'version'.)
 Will dWritefln(..){} compile to 'nothing'?
 I mean, will calling dWritefln when DEBUGG=false take no cpu tick

 compiler see that it does nothing)?

Yes. See thread entitled "debug()" over in digitalmars.D. digitalmars.com digitalmars.D:66826 digitalmars.com digitalmars.D:66827 But that may only be with -inline. Not sure what flags Jarrett used when he ran the test. --bb

Mar 20 2008