Thread overview
[Issue 9872] New: format should print class fields
Apr 04, 2013
Andrej Mitrovic
Apr 04, 2013
Andrej Mitrovic
[Issue 9872] format should include class field values
Apr 04, 2013
Andrej Mitrovic
Apr 04, 2013
Andrej Mitrovic
April 04, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9872

           Summary: format should print class fields
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: andrej.mitrovich@gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-04-04 00:27:25 PDT ---
This is an unfortunate inconsistency:

import std.stdio;
import std.string;

struct S
{
    int x, y;
}

class C
{
    int x, y;
}

void main()
{
    auto s1 = format("%s", S());
    auto s2 = format("%s", new C());

    writeln(s1);  // S(0, 0)
    writeln(s2);  // test.C
}

It forces us to either always define a toString() method or call some custom user-provided formatting function. format() should try to print the values of the class fields if the toString method is not defined.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 04, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9872


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 04, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9872



--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-04-04 00:32:34 PDT ---
Workaround:

private mixin template genToString()
{
    override string toString()
    {
        import std.array;
        import std.conv;
        import std.string;

        Appender!(string[]) result;

        foreach (val; this.tupleof)
        {
            result ~= to!string(val);
        }

        return format("%s(%s)", __traits(identifier, typeof(this)),
                                result.data.join(", "));
    }
}

class C
{
    int x, y;
    mixin genToString;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 04, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9872



--- Comment #2 from Andrej Mitrovic <andrej.mitrovich@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: -------