digitalmars.D - new string mixins -- debug printfs
- Bill Baxter (31/31) Feb 06 2007 Here's something fun that's now possible:
- Walter Bright (5/41) Feb 06 2007 Ah, I hadn't realize that this does solve the "point of instantiation"
- BCS (5/11) Feb 06 2007 i'd use
- Walter Bright (7/22) Feb 06 2007 or:
- Bradley Smith (7/31) Feb 06 2007 Typo. Missing ';' closing writefln statement. Should be the following.
Here's something fun that's now possible: version(trace) { template Trace(char[] msg="") { const char[] Trace = `writefln("%s(%s): `~msg~`", __FILE__, __LINE__);`; } } else { template Trace(char[] str="") { const char[] Trace = ""; } } Zero overhead tracing when disabled, file and line number automatically supplied when enabled. But just from trying to do this little thing, it's pretty clear to me that "my head asplode" if I have to write too much of this kinda thing without a better string literal syntax geared to this. Something like: const char[] Trace = """ writefln("%s(%s): (msg)", __FILE__, __LINE__); """ ; Would be much less likely to make me want to scratch my eyes out. Also a little buglet -- You can call the above like mixin(Trace!("a_function")); or mixin(Trace!()); But this doesn't work: mixin(Trace); --bb
Feb 06 2007
Bill Baxter wrote:Here's something fun that's now possible: version(trace) { template Trace(char[] msg="") { const char[] Trace = `writefln("%s(%s): `~msg~`", __FILE__, __LINE__);`; } } else { template Trace(char[] str="") { const char[] Trace = ""; } } Zero overhead tracing when disabled, file and line number automatically supplied when enabled.Ah, I hadn't realize that this does solve the "point of instantiation" expansion with __FILE__ and __LINE__ <g>.But just from trying to do this little thing, it's pretty clear to me that "my head asplode" if I have to write too much of this kinda thing without a better string literal syntax geared to this. Something like: const char[] Trace = """ writefln("%s(%s): (msg)", __FILE__, __LINE__); """ ; Would be much less likely to make me want to scratch my eyes out.Yes, a better string literal syntax will probably have to be devised.Also a little buglet -- You can call the above like mixin(Trace!("a_function")); or mixin(Trace!()); But this doesn't work: mixin(Trace);It's not a bug, it's as designed.
Feb 06 2007
Bill Baxter wrote:Here's something fun that's now possible: version(trace) { template Trace(char[] msg="") { const char[] Trace = `writefln("%s(%s): `~msg~`", __FILE__, __LINE__);`;i'd use `writefln(__FILE__"("~itoa!(__LINE__)~"): %s), "`~msg~`");`; file and line get folded and % dosn't mess thing up COOL
Feb 06 2007
BCS wrote:Bill Baxter wrote:or: const char[] Trace = std.metastrings.Format!( `writefln("%%s", "%s(%s): %s")`, __FILE__, __LINE__, msg );Here's something fun that's now possible: version(trace) { template Trace(char[] msg="") { const char[] Trace = `writefln("%s(%s): `~msg~`", __FILE__, __LINE__);`;i'd use `writefln(__FILE__"("~itoa!(__LINE__)~"): %s), "`~msg~`");`; file and line get folded and % dosn't mess thing up COOL
Feb 06 2007
Walter Bright wrote:BCS wrote:Typo. Missing ';' closing writefln statement. Should be the following. const char[] Trace = std.metastrings.Format!( `writefln("%%s", "%s(%s): %s");`, __FILE__, __LINE__, msg );Bill Baxter wrote:or: const char[] Trace = std.metastrings.Format!( `writefln("%%s", "%s(%s): %s")`, __FILE__, __LINE__, msg );Here's something fun that's now possible: version(trace) { template Trace(char[] msg="") { const char[] Trace = `writefln("%s(%s): `~msg~`", __FILE__, __LINE__);`;i'd use `writefln(__FILE__"("~itoa!(__LINE__)~"): %s), "`~msg~`");`; file and line get folded and % dosn't mess thing up COOL
Feb 06 2007