www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9872] New: format should print class fields

reply d-bugmail puremagic.com writes:
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



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: -------
Apr 04 2013
next sibling parent d-bugmail puremagic.com writes:
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: -------
Apr 04 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9872




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: -------
Apr 04 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9872




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: -------
Apr 04 2013