module test;
import std.algorithm;
import std.stdio;
import std.conv;
struct Bar
{
int x;
int y;
}
struct Foo
{
Bar bar;
int xFoo;
int yFooBar;
}
void main()
{
echo(Foo());
// prints:
// Bar.x = 0
// Bar.y = 0
// Foo.xFoo = 0
// Foo.yFooBar = 0
}
void echo(T)(T arg) if (is(T == struct) || is(T == class))
{
string[2][] results;
auto fields = arg.tupleof;
foreach (i, field; fields)
{
static if (is(typeof(field) == struct) || is(typeof(field) == class))
{
echo(field);
}
else
{
string fieldName = typeof(arg).tupleof[i].stringof;
auto periodLoc = fieldName.countUntil(')');
fieldName = fieldName[1..periodLoc] ~ fieldName[periodLoc+1..$];
results ~= [fieldName, to!string(field)];
}
}
size_t indent;
foreach (res; results)
{
indent = max(indent, res[0].length);
}
foreach (res; results)
{
writefln("%-" ~ to!string(indent) ~ "s = %s", res[0], res[1]);
}
}