Getting nice print of struct for debugging

Martin Tschierschke via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Feb 22 03:18:15 PST 2017


On Tuesday, 21 February 2017 at 14:02:54 UTC, Jacob Carlborg 
wrote:
[...]
> Yes, this works, I would say this is the simplest:
>
> MyStruct s;
>
> foreach (index, name ; FieldNameTuple!MyStruct)
>     writefln("%s: %s", name, s.tupleof[index]);
>
> If you want something more close to "send" in Ruby, you need to 
> use a string mixin, like this:
>
> foreach (name ; FieldNameTuple!MyStruct)
>     writefln("%s: %s", name, mixin("s." ~ name));
>
> The string mixin example works for methods, opDispatch and 
> similar as well. The tupleof example, the first one, works only 
> for fields.
Exactly what I was looking for, **thank you!**
Both ways of accessing the struct elements are very interesting,
giving an impression what is possible with D.


Is it possible to overwrite "toString" for all structs in one 
step?

Regards mt.




More information about the Digitalmars-d-learn mailing list