[Issue 9872] format should include class field values

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Apr 4 11:49:21 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=9872



--- Comment #2 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2013-04-04 11:49:19 PDT ---
How's this for a funky workaround:

diff --git a/std/format.d b/std/format.d
index 8896e38..84169c0 100644
--- a/std/format.d
+++ b/std/format.d
@@ -2512,15 +2512,32 @@ if (is(T == class) && !is(T == enum))
         put(w, "null");
     else
     {
+        Object o = val;     // workaround
+        string delegate() dg = &o.toString;
+
         static if (hasToString!(T, Char) > 1 || (!isInputRange!T &&
!is(BuiltinTypeOf!T)))
         {
-            formatObject!(Writer, T, Char)(w, val, f);
+            if (dg.funcptr != &Object.toString)
+                formatObject!(Writer, T, Char)(w, val, f);
+            else
+            {
+                enum ident = __traits(identifier, T);
+
+                mixin(format(q{
+                    static struct %s
+                    {
+                        typeof(T.tupleof) fields;
+                    }
+                    %s s;
+                    s.fields = val.tupleof;
+                }, ident, ident));
+
+                formatValue(w, s, f);
+            }
         }
         else
         {
           //string delegate() dg = &val.toString;
-            Object o = val;     // workaround
-            string delegate() dg = &o.toString;
             if (dg.funcptr != &Object.toString) // toString is overridden
             {
                 formatObject(w, val, f);

Yeah it's just a joke. But it works. :P

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list