www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6521] New: writeln(const(tuple)) doesn't show the field values

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6521

           Summary: writeln(const(tuple)) doesn't show the field values
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



import std.stdio: writeln;
import std.typecons: tuple;
void main() {
    auto r1 = tuple(1);
    writeln(r1);
    const r2 = tuple(1);
    writeln(r2);
}


2.055beta prints:

Tuple!(int)(1)
const(Tuple!(int))


But I'd like to see the values of the fields (1) in the second case too.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 17 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6521




Expected prints:

Tuple!(int)(1)
const(Tuple!(int))(1)

There are two ways to fix the problem.

1. Change std.typecons.Tuple!T.toString like follows:

diff --git a/std/typecons.d b/std/typecons.d
index b279abf..b80d6f9 100644
--- a/std/typecons.d
+++ b/std/typecons.d
   -491,9 +491,9    assert(s[0] == "abc" && s[1] == 4.5);
 /**
    Converts to string.
  */
-    string toString()
+    const string toString(this T)()
     {
-        enum header = typeof(this).stringof ~ "(",
+        enum header = T.stringof ~ "(",
              footer = ")",
              separator = ", ";
         Appender!string app;

But this is very hackish to me.

2. Apply my pull request
https://github.com/D-Programming-Language/phobos/pull/126
This patch changes formattedWrite behavior: if a struct type does not have
toString method, formattedWrite formats it like POD (e.g. "TypeName(field,
...)").

I think that Tuple is just a POD type, and expected formatting also looks like
the POD.
So, IMHO, std.typecons.Tuple!T.toString is *just a workaround*.
If formattedWrite works correctly, we will no longer need Tuple!T.toString.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 17 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6521


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED



Fixed.

https://github.com/D-Programming-Language/phobos/commit/4c8cbd2f29637abfadb2d3057a5e747fe8084d4d

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