Convenient debug printing

Rubn where at is.this
Sat Feb 2 00:06:58 UTC 2019


On Friday, 1 February 2019 at 15:49:24 UTC, Per Nordlöw wrote:
> Rust just added a standard macro `dbg` for debug printing
>
> https://blog.rust-lang.org/2019/01/17/Rust-1.32.0.html#the-dbg-macro
>
> What's the closest match for this macro we can get with D?
>
>
> I have put together a similar solution at
>
>     
> https://github.com/nordlow/phobos-next/blob/master/src/dbgio.d#L51
>
> used as
>
>     int x = 42;
>     dbg("x: ", x);
>
> printing
>
>     dbgio.d:68: Info: x: 42
>
> but it only works with a cumbersome writeln-syntax.
>
>
> I also have
>
>     
> https://github.com/nordlow/phobos-next/blob/master/src/dbgio.d#L12
>
> copied from Wilzbach's effort and used as
>
>     int x = 42;
>     int[] y = [42, 43];
>     mixin dump!("x", "y");
>
> printing
>
>     dbgio.d:17: Info: x: 42, y: [42, 43]
>
> but, of course, cumbersome in another syntactic way.
>
>
> Is this as good as it gets in D with regards to syntactic 
> sugarness?

import std.stdio;

void dbg(alias a)(string file = __FILE__, int line = __LINE__) {
	writeln( file, "(", line, "): ", __traits(identifier, a), " = ", 
a );
}

int value = 10;
dbg!value;      // filename.d(12): value = 10




More information about the Digitalmars-d mailing list