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

Andrej Mitrovic andrej.mitrovich at gmail.com
Sat Apr 9 10:31:51 PDT 2011


On 4/9/11, Robert Clipsham <robert at octarineparrot.com> wrote:
> 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/
>

That's great, I can use it to print out all the fields. Thanks!


More information about the Digitalmars-d-learn mailing list