Anyone have a function to print out the field name and its value?

Andrej Mitrovic none at none.none
Sat Apr 9 10:13:23 PDT 2011


Let's say I have this structure:

struct PAINTSTRUCT
{
    bool state;
}

And somewhere in my main code I want to print out the value of state. But I also want to know what I'm printing out. So usually I'd do this:
void main()
{
    PAINTSTRUCT ps;
    writefln("ps.state = %s", ps.state);
}

Has anyone written a function which will automatically print out both the variable name and any object it might be part of, and then the value?

E.g. I'd like to use a function like this:
writeField(ps.state);

And if state is false it would print out to the console:
"ps.state = false"

I can't really touch PRINTSTRUCT because it's already defined as a WinAPI structure and messing with that would be bad, so I can't implement toString() or any helper functions within the struct. I need an outside function which could do this automatically for any object/struct type.


More information about the Digitalmars-d-learn mailing list