Debug function / macro

Paul Backus snarwin at gmail.com
Mon Feb 3 23:55:42 UTC 2020


On Monday, 3 February 2020 at 22:07:13 UTC, ag wrote:
> Does D have a debugging helper function or macro similar to 
> this one in Rust? (https://doc.rust-lang.org/std/macro.dbg.html)
>
> If not, how would one implement this / how difficult would it 
> be to implement?

Here's one way you could do it:

string debugPrint(string expr, string file = __FILE__, size_t 
line = __LINE__)
{
     import std.format;

     return q{() {
         import std.stdio: writeln;
         debug writeln(`[%2$s:%3$d] %1$s`, " = ", %1$s);
     }();}.format(expr, file, line);
}

void main()
{
     int x = 123;
     int y = 456;
     mixin(debugPrint("x + y"));
}


More information about the Digitalmars-d-learn mailing list