Anyone have a function to print out the field name and its value?

Andrej Mitrovic andrej.mitrovich at gmail.com
Sat Apr 9 10:30:46 PDT 2011


Wow, I figured out a trick. Check it out, two modules:

1. fieldwrite.d:
module fieldwrite;

import std.string;
import std.stdio;
import std.conv;

mixin template field(string T)
{
    struct FieldTemp
    {
        this(string str)
        {
            writefln(str ~ " = %s", mixin(T));
        }
    }

    FieldTemp fieldTemp = FieldTemp(T);
}

2. driver.d:

import std.stdio;
import fieldwrite;

struct S
{
    int x;
    int y;
}

void main()
{
    S s;
    s.x = 1;
    s.y = 2;

    mixin field!"s.x";
    mixin field!"s.y";
}


More information about the Digitalmars-d-learn mailing list