Generate toString() method at compile time

Adam D. Ruppe destructionator at gmail.com
Wed Mar 26 07:47:29 PDT 2014


On Wednesday, 26 March 2014 at 14:16:08 UTC, Ary Borenszweig 
wrote:
> I'd like to generate a toString() method for my classes. For 
> example:

Try this:

   override string toString() {
         import std.conv;
         string s;
         s ~= this.classinfo.name ~ "(";
         foreach(idx, val; this.tupleof) {
                 if(idx) s ~= ", ";
                 s ~= this.tupleof[idx].stringof[5 .. $];
                 s ~= " = ";
                 s ~= to!string(val);
         }
         s ~= ")";
         return s;
   }

add that method to Foo.

$ ./test58
test58.Foo(x = 1, y = 2)


More information about the Digitalmars-d-learn mailing list