Generate toString() method at compile time

Ary Borenszweig ary at esperanto.org.ar
Wed Mar 26 07:16:07 PDT 2014


Hi,

I'd like to generate a toString() method for my classes. For example:

---
import std.stdio;

class Foo {
   int x;
   int y;

   // mixin ...?
}

void main() {
   auto foo = new Foo;
   foo.x = 1;
   foo.y = 2;
   writeln(foo); // prints "Foo(x = 1, y = 2)"
}
---

I tried using getAllMembers, but that also gives me all methods of Foo. 
I just want the fields. I also tried FieldTypeTuple 
(http://dlang.org/phobos/std_traits.html#FieldTypeTuple) but I get that 
gives me the types. And then I can't find anything else in std.traits 
that could help me.

Is this possible?

Thanks,
Ary


More information about the Digitalmars-d-learn mailing list