tuples and names

Kevin Bealer kevinbealer at gmail.com
Thu Nov 16 14:58:32 PST 2006


I wonder if it's possible to get the names of fields of a struct?  What I'm
imagining is this kind of usage:

struct Foo {
   int x;
   char[] y;
};

template(T) {
  void PrintAsXml(T z)
  {
      char[] name = z.fieldnamesof;

      alias ParameterTypeTuple!(T) TP;

      foreach(auto A; z.tupleof) {
          writefln("<%s>%s</%s>", name, A, name);
      }
  }
}

Which might print (assuming z.x = 5 and z.y = "hello"):
<x>5</x>
<y>hello</y>

This might also be a way to look for a function by a given name, as with
Java's reflective capability, althought I would imagine data field names would
be a seperate operator than method names.

Kevin



More information about the Digitalmars-d mailing list