Debug arguments?

bearophile via Digitalmars-d digitalmars-d at puremagic.com
Tue May 13 02:38:50 PDT 2014


Sometimes you want to give arguments to a function that are only 
used for debug builds. So is it a good idea to introduce debug 
arguments (only allowed as trailing arguments, like the arguments 
with a default value)?


import std.stdio;
void foo(ref int x, debug int y) {
     x++;
     debug writeln(y);
}
void main() {
     int a, b;
     foo(a, debug b);
}


That is equivalent to code like:

import std.stdio;
debug {
     void foo(ref int x, int y) {
         x++;
         writeln(y);
     }
} else {
     void foo(ref int x) {
         x++;
     }
}
void main() {
     int a, b;
     debug {
         foo(a, b);
     } else {
         foo(a);
     }
}


This avoids to pass useless arguments, and it documents (in the 
code) that certain arguments are not used in a function in 
non-debug builds.

(The "debug" at the calling point is not necessary, but it 
documents better the meaning of the code).

Bye,
bearophile


More information about the Digitalmars-d mailing list