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

Robert Clipsham robert at octarineparrot.com
Sat Apr 9 10:26:41 PDT 2011


On 09/04/2011 18:23, Robert Clipsham wrote:
> Off the top of my head (untested):
> ----
> void print(T)(T t) if (is(T == struct) || is(T == class))
> {
> foreach (i, field; t.tupleof)
> {
> writefln(T.tupleof[i].stringof ~ " = %s", field);
> }
> }
> ----
>
> --
> Robert
> http://octarineparrot.com/

I forgot to mention... Usage:
---
print(myStruct);
----

It's also simple enough to adapt to just print a given field etc.

-- 
Robert
http://octarineparrot.com/


More information about the Digitalmars-d-learn mailing list