digitalmars.D.learn - easy way to output a struct?
- Hoenir (4/4) Jan 14 2009 Is there an easy way to output a struct's contents?
- Jarrett Billingsley (3/7) Jan 14 2009 bearophile has apparently done so in his "libs" but I have no idea
- bearophile (5/7) Jan 15 2009 If you use Phobos on D1 the d.string.put/putr to print generic structs, ...
- BCS (2/6) Jan 14 2009 http://codepad.org/Eu16XqFu
- Hoenir (18/20) Jan 14 2009 Thank you very much, it works like a charm.
- Denis Koroskin (4/24) Jan 15 2009 One note is that you should probably pass the object by reference:
- Hoenir (5/10) Jan 15 2009 Good idea. btw ref'ing a class has no effect, has it?
- BCS (9/22) Jan 15 2009 ref object would allow you to alter what the passed in argument is:
- Hoenir (3/23) Jan 15 2009 Oh, yeah of course, forgot that. Was just thinking of structs being
- Hoenir (7/14) Jan 15 2009 Why is that "is" used here:
- Jarrett Billingsley (12/18) Jan 15 2009 is(blah blah) is a completely different thing than "x is y" ;)
- Hoenir (3/3) Jan 15 2009 Thanks for your explanation.
- Jarrett Billingsley (2/5) Jan 16 2009 Yeah. http://www.digitalmars.com/d/1.0/expression.html#IsExpression
- BCS (9/27) Jan 16 2009 the code with some annotations. It should be pointed out that the code i...
- bearophile (4/5) Jan 15 2009 The stuff I have linked you is recursive, of course.
- Hoenir (3/8) Jan 15 2009 Thanks. That's a whole lot of stuff.
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.
Jan 14 2009
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.
Jan 14 2009
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
Jan 15 2009
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
Jan 14 2009
BCS schrieb:http://codepad.org/Eu16XqFuThank 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?
Jan 14 2009
On Thu, 15 Jan 2009 03:04:15 +0300, Hoenir <mrmocool gmx.de> wrote:BCS 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) { ... } // D2http://codepad.org/Eu16XqFuThank 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?
Jan 15 2009
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) { ... } // D2Good 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.
Jan 15 2009
Reply to Hoenir,Denis Koroskin schrieb: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);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) { ... } // D2Good 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.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.
Jan 15 2009
BCS schrieb:Reply to Hoenir,Oh, yeah of course, forgot that. Was just thinking of structs being value and classes being reference types.Denis Koroskin schrieb: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);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) { ... } // D2Good idea. btw ref'ing a class has no effect, has it?
Jan 15 2009
BCS schrieb: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.Why is that "is" used here: static if(is(a == char*)) I know is is normally used for identity comparison, but what does it do here? Also, I've never seen something like that (I mean that ":"): else static if(is(a : int))
Jan 15 2009
On Thu, Jan 15, 2009 at 6:57 PM, Hoenir <mrmocool gmx.de> wrote:Why is that "is" used here: static if(is(a == char*)) I know is is normally used for identity comparison, but what does it do here?is(blah blah) is a completely different thing than "x is y" ;) is() is used to test, at compile time, for some type-related information. It also has some stranger forms that allows it to introduce symbols that are bound to smaller pieces of types (for example, it can be used to get the return type or parameter types of a function or delegate). is(a == char*) returns true if the type 'a' is char*, and false otherwise.Also, I've never seen something like that (I mean that ":"): else static if(is(a : int))The : is similar to ==, but means "implicitly." While is(a == int) returns true only if the type 'a' is int, is(a : int) returns true if the type 'a' is _implicitly_ convertible to int, including things like short and dchar.
Jan 15 2009
Thanks for your explanation. Is there any good resource about that stuff other than http://www.digitalmars.com/d/1.0/template.html
Jan 15 2009
On Fri, Jan 16, 2009 at 2:46 AM, Hoenir <mrmocool gmx.de> wrote:Thanks for your explanation. Is there any good resource about that stuff other than http://www.digitalmars.com/d/1.0/template.htmlYeah. http://www.digitalmars.com/d/1.0/expression.html#IsExpression
Jan 16 2009
Reply to Hoenir,BCS schrieb:the code with some annotations. It should be pointed out that the code is only designed to deal with C types) // If the variable is a C-string, print it as such static if(is(a == char*)) writef(`, "%s"`, t[i][0..strlen(t[i])]); // if the variable is implicitly castable to int print it as such else static if(is(a : int)) writef(", %d", t[i]); // otherwise print the type name and the value (hope write can deal with it) else writef(", [%s]%s", a.stringof,t[i]);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.Why is that "is" used here: static if(is(a == char*)) I know is is normally used for identity comparison, but what does it do here? Also, I've never seen something like that (I mean that ":"): else static if(is(a : int))
Jan 16 2009
Hoenir:So it needs to be recursive somehow.The stuff I have linked you is recursive, of course. Bye, bearophile
Jan 15 2009
bearophile schrieb:Hoenir:Thanks. That's a whole lot of stuff. Guess I really need to dig deeper into template stuff.So it needs to be recursive somehow.The stuff I have linked you is recursive, of course.
Jan 15 2009