easy way to output a struct?
Denis Koroskin
2korden at gmail.com
Thu Jan 15 01:05:11 PST 2009
On Thu, 15 Jan 2009 03:04:15 +0300, Hoenir <mrmocool at gmx.de> wrote:
> BCS schrieb:
>> http://codepad.org/Eu16XqFu
>>
>
> Thank you very much, it works like a charm.
> I wrote a small function to log whatever object I pass to it:
>
> void log(T)(T obj)
> {
> static if (is(T == struct) || is(T == class))
> {
> writef("{");
> foreach(i,_;obj.tupleof)
> writefln("%s : %s,", obj.tupleof[i].stringof[4..$], obj.tupleof[i]);
> writefln("}");
> }
> else
> {
> writefln(obj);
> }
> }
>
> Are there better solutions than this or does it perhaps have any flaws?
One note is that you should probably pass the object by reference:
void log(T)(ref T obj) { ... } // D1
void log(T)(ref const(T) obj) { ... } // D2
More information about the Digitalmars-d-learn
mailing list