www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - I'm Dumpin' it!

reply Seb <seb wilzba.ch> writes:
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
May 12 2017
next sibling parent Andrea Fontana <nospam example.com> writes:
On Friday, 12 May 2017 at 12:55:37 UTC, Seb wrote:
 1) Do you think there should be language / compiler support for 
 dumping variables nicely?
Yes
 2) Would you use dump!(x, y)?
YES!
 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?
It's ok if it skip those checks on debug block: debug { dump!(x,y); }
May 12 2017
prev sibling parent reply ixid <adamsibson gmail.com> writes:
On Friday, 12 May 2017 at 12:55:37 UTC, Seb wrote:
 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.
I think the word dump would be too confusing for users based on the history of the term in C. It should be writeFormattedVariableLn. /sarcasm. Just parodying the frankly ridiculous response to 'print'.
May 12 2017
parent reply Meta <jared771 gmail.com> writes:
On Friday, 12 May 2017 at 13:50:50 UTC, ixid wrote:
 I think the word dump would be too confusing for users based on 
 the history of the term in C. It should be 
 writeFormattedVariableLn. /sarcasm.

 Just parodying the frankly ridiculous response to 'print'.
In all seriousness, debugPrint or something similar might be a more informative name. Otherwise, I like the functionality. I've written my own version of dump before in a couple projects and it'd be nice to have it in Phobos. Also as Andrea said, does `debug` obviate the need for dump to be nogc nothrow safe? Does the following work? void test(int x) nogc nothrow safe pure { debug dump!x(); } As for it being pure, I see no reason why or how dump can be pure.
May 12 2017
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 5/12/17 10:10 AM, Meta wrote:
 On Friday, 12 May 2017 at 13:50:50 UTC, ixid wrote:
 I think the word dump would be too confusing for users based on the
 history of the term in C. It should be writeFormattedVariableLn.
 /sarcasm.

 Just parodying the frankly ridiculous response to 'print'.
In all seriousness, debugPrint or something similar might be a more informative name. Otherwise, I like the functionality. I've written my own version of dump before in a couple projects and it'd be nice to have it in Phobos. Also as Andrea said, does `debug` obviate the need for dump to be nogc nothrow safe? Does the following work? void test(int x) nogc nothrow safe pure { debug dump!x(); }
AFAIK, debug only affects pure. You can call non-pure functions inside a debug block while inside a pure function. -Steve
May 12 2017