Thread overview | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 14, 2009 easy way to output a struct? | ||||
---|---|---|---|---|
| ||||
Is there an easy way to output a struct's contents? e.g. member1: value1, member2: value2, ... I have a really big struct and formatting it all by hand would be time-consuming. |
January 14, 2009 Re: easy way to output a struct? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hoenir | On Wed, Jan 14, 2009 at 4:57 PM, Hoenir <mrmocool@gmx.de> wrote:
> Is there an easy way to output a struct's contents?
> e.g. member1: value1, member2: value2, ...
> I have a really big struct and formatting it all by hand would be
> time-consuming.
>
bearophile has apparently done so in his "libs" but I have no idea where you can download that.
|
January 14, 2009 Re: easy way to output a struct? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hoenir | Reply to Hoenir, > Is there an easy way to output a struct's contents? > e.g. member1: value1, member2: value2, ... > I have a really big struct and formatting it all by hand would be > time-consuming. http://codepad.org/Eu16XqFu |
January 15, 2009 Re: easy way to output a struct? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | 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?
|
January 15, 2009 Re: easy way to output a struct? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hoenir | On Thu, 15 Jan 2009 03:04:15 +0300, Hoenir <mrmocool@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
|
January 15, 2009 Re: easy way to output a struct? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley: > bearophile has apparently done so in his "libs" but I have no idea where you can download that. If you use Phobos on D1 the d.string.put/putr to print generic structs, or use a d.templates.Record struct: http://www.fantascienza.net/leonardo/so/libs_d.zip Bye, bearophile |
January 15, 2009 Re: easy way to output a struct? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Denis Koroskin | Denis Koroskin schrieb:
> 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
>
Good idea. btw ref'ing a class has no effect, has it?
Actually discovered a flaw, if you e.g. pass an array of structs to it, dmd complains about the struct not having a toString() member.
So it needs to be recursive somehow.
|
January 15, 2009 Re: easy way to output a struct? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hoenir | Reply to Hoenir, > Denis Koroskin schrieb: > >> 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 > Good idea. btw ref'ing a class has no effect, has it? > ref object would allow you to alter what the passed in argument is: void Fn(ref object o) { o = null; } object o = new Bah!(); Fn(o); assert(o is null); > Actually discovered a flaw, if you e.g. pass an array of structs to > it, dmd complains about the struct not having a toString() member. > > So it needs to be recursive somehow. > I have a partial solution here: http://www.dsource.org/projects/scrapple/browser/trunk/log_api/LogAPI.d Feel free to steal whatever you want from it. If you are really adventures and want to, I can get you SVN access and you can dump stuff back into that. |
January 15, 2009 Re: easy way to output a struct? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hoenir | Hoenir:
> So it needs to be recursive somehow.
The stuff I have linked you is recursive, of course.
Bye,
bearophile
|
January 15, 2009 Re: easy way to output a struct? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS schrieb:
> Reply to Hoenir,
>
>> Denis Koroskin schrieb:
>>
>>> 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
>> Good idea. btw ref'ing a class has no effect, has it?
>>
>
> ref object would allow you to alter what the passed in argument is:
>
>
> void Fn(ref object o) { o = null; }
>
> object o = new Bah!();
> Fn(o);
> assert(o is null);
>
Oh, yeah of course, forgot that. Was just thinking of structs being value and classes being reference types.
|
Copyright © 1999-2021 by the D Language Foundation