Hi all,

last year there has been a discussion about adding a utility method for easily dumping variables during debugging to Phobos [3971]. The effort stalled and after a couple of months I tried to reboot it [4318]. Now this PR got stalled as well and I am rethinking the work. I think dump!(x, y) already got pretty close to the ideal solution and as we probably aren't going to add a new keyword or language feature for this, I think this is the best what we can get atm.
Hence, I am interested on your thoughts on this topic:
1) Do you think there should be language / compiler support for dumping variables nicely?
2) Would you use dump!(x, y)?
3) Should there be a version of dump that is set to be `@nogc @safe nothrow pure`, s.t. it can be inserted anywhere for handy debugging?

For reference, this is how the proposed dump function would look like (for more infos see [4318]).

```
int x = 5, y = 3;

// dump! is customizable like std.format.formatValue
assert(dump!(x, y)("%s = %s, ") == "x = 5, y = 3");

// this is also the default behavior
assert(dump!(x, y) == "x = 5, y = 3");

// with order
assert(dump!(x, y)("%2$s = %1$s, ") == "5 = x, 3 = y");

// with runtime args
assert(dump!(x, y)("%s = %s, ", () => 42) == "x = 5, y = 3, () = 42");

// with runtime args & position-specifier
assert(dump!(x, y)("%1$s = %2$s; ", "var1") == "x = 5; y = 3; 0 = var1");

// with types
assert(dump!(x, y)("(%s: %3$s) = %2$s, ") == "(x: int) = 5, (y: int) = 3"); assert(dump!(x, y)("(%s!%3$s) = %2$s, ") == "(x!int) = 5, (y!int) = 3");

// custom separator
assert(dump!(x, y)("%s = %s; ") == "x = 5; y = 3");

// all printf formatting commands work
assert(dump!(x, y)("%-4s = %4s, ") == "x    =    5, y    =    3");

// special formatting (if applicable for all types)
auto z1 = 2.0, z2 = 4.0;
assert(dump!(z1, z2)("%s = %.3f & ") == "z1 = 2.000 & z2 = 4.000");

// functions
assert(dump!(x, y, () => x + y)("%s = %s; ") == "x = 5; y = 3; () = 8");

// runtime paramters
auto b = (int a) => ++a;
assert(dump!(x, y)("%s = %s, ", b(x), x - y) == "x = 5, y = 3, 0 = 6, 1 = 2");

// validate laziness
auto c = (ref int a) => ++a;
assert(dump!(x, y, () => x + y)("%s = %s, ", c(x), x - y) == "x = 5, y = 3, () = 8, 0 = 6, 1 = 3"); assert(dump!(x, y, () => x + y)("%s = %s, ", c(x), x - y) == "x = 6, y = 3, () = 9, 0 = 7, 1 = 4");

// use any output range
import std.stdio : stdout;
dump!(x, y)(stdout.lockingTextWriter(), "%s = %s, ");
```


[3971]: https://github.com/dlang/phobos/pull/3971
[4318]: https://github.com/dlang/phobos/pull/4318

Reply via email to