Temporarily disable all purity for debug prints

Steven Schveighoffer schveiguy at yahoo.com
Tue Apr 12 08:03:19 PDT 2011


On Mon, 11 Apr 2011 17:27:41 -0400, bearophile <bearophileHUGS at lycos.com>  
wrote:

> From what I am seeing, in a D2 program if I have many (tens or more)  
> pure functions that call to each other, and I want to add (or activate)  
> a printf/writeln inside one (or few) of those functions to debug it, I  
> may need to temporarily comment out the "pure" attribute of many  
> functions (because printing can't be allowed in pure functions).
>
> As more and more D2 functions become pure in my code and in Phobos,  
> something like a -disablepure compiler switch (and printf/writeln inside  
> debug{}) may allow more handy debugging with prints (if the purity is  
> well managed by the compiler then I think disabling the pure attributes  
> doesn't change the program output).

use C linkage, you can do whatever you want.  It's how druntime  
accomplishes a lot of stuff.

For example:

puredebug.d:

import std.stdio;

extern(C) void pureprint(string[] values...)
{
    write("[pure debug] ");
    foreach(value; values)
      write(value);
    writeln();
}

puredebug.di:

extern(C) void pureprint(string[] values...) pure;

Should work.

-Steve


More information about the Digitalmars-d mailing list