On Sunday, 29 September 2013 at 14:31:15 UTC, linkrope wrote:
I want to pretty-print the representation of a value of a
generic type T.
In Ruby, I would use 'pp':
value = 'hello'
pp value # prints "hello" - with quotes!
value = 42
pp value # prints 42
Now, value.to!string eliminates the quotes, should value be of
type string.
As a workaround, I put the value into an array to make use of
the "undocumented" function formatElement:
"%(%s%)".format([value])
Ugly! Where does Phobos hide the function I'm looking for?
I have one at:
https://github.com/patefacio/d-help/blob/master/d-help/pprint/pp.d
The following code outputs the text below:
import std.stdio;
import pprint.pp;
enum Color {
Red,
White,
Blue
}
struct R {
int x = 22;
string s = "foobar";
}
struct S {
int i;
R r;
}
struct T {
int []i;
string []j;
}
void main() {
auto s = S(3);
auto t = T([1,2,3], ["a", "b", "c"]);
writeln(pp(Color.Red));
writeln(pp(42));
writeln(pp("hello"));
writeln(pp(s));
writeln(pp(t));
}
Outputs
Red
42
"hello"
{
(S).i = 3
(S).r = {
(R).x = 22
(R).s = "foobar"
}
}
{
(T).i = [
[0]->1
[1]->2
[2]->3
]
(T).j = [
[0]->"a"
[1]->"b"
[2]->"c"
]
}