Printing struct fields and values

z z at mail.com
Thu Jan 22 16:11:19 UTC 2026


A simple function to dump a struct's fields:
```dlang
void inspect_struct(T)(T t)
{
    (T).stringof.writeln;
    foreach(i, member; FieldNameTuple!T)
    {
      if (isPointer!(Fields!T[i]))
      {
        // error (sometimes)
        writefln("%s %s = %s", Fields!T[i].stringof, member, 
(*t.tupleof[i]));
      }

      else
      {
        writefln("%s %s = %s", Fields!T[i].stringof, member, 
(t.tupleof[i]));
      }

    }
}
```
I would also like to automatically deref any pointers for their 
values.
This works if all fields in a struct are pointers.
But for any fields that aren't, you get an error:
```
Error: can only `*` a pointer, not a `ushort`
```
What am I missing / What would be the proper way to do this?


More information about the Digitalmars-d-learn mailing list