Thread overview
[Issue 6231] New: [patch] std.conv.to: Structs with toString and isInputRange match multiple templates.
Jun 30, 2011
Rob Jacques
[Issue 6231] [patch] std.conv.to/std.format.: Structs with toString and isInputRange match multiple templates.
Jun 30, 2011
Rob Jacques
Jun 14, 2012
Kenji Hara
June 30, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6231

           Summary: [patch] std.conv.to: Structs with toString and
                    isInputRange match multiple templates.
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Keywords: patch
          Severity: normal
          Priority: P3
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: sandford@jhu.edu


--- Comment #0 from Rob Jacques <sandford@jhu.edu> 2011-06-30 11:27:03 PDT ---
If a struct defines a custom toString method and satisfies isInputRange, then to!string(Struct) will match multiple toImpl templates. The solution is to add an extra template constraint to the input range toImpl to detect structs with custom toString methods.

[Line 194 in DMD 2.053]
T toImpl(T, S)(S s, in T leftBracket = "[", in T separator = ", ",
    in T rightBracket = "]")
if (isSomeString!T && !isSomeChar!(ElementType!S) &&
(isInputRange!S || isInputRange!(Unqual!S)) )

-This is the constraint to add.
&& !(is(S == struct) && is(typeof(&S.init.toString))) )

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 30, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6231


Rob Jacques <sandford@jhu.edu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[patch] std.conv.to:        |[patch]
                   |Structs with toString and   |std.conv.to/std.format.:
                   |isInputRange match multiple |Structs with toString and
                   |templates.                  |isInputRange match multiple
                   |                            |templates.


--- Comment #1 from Rob Jacques <sandford@jhu.edu> 2011-06-30 12:04:07 PDT ---
std.format.formatRange also suffers from a similar problem, although in this case it is one of choosing a non-character range over the custom toString routine.

[Line 1171 in format.d in DMD 2.053]
void formatValue(Writer, T, Char)(Writer w, T val,
        ref FormatSpec!Char f)
if (isInputRange!T && !isSomeString!T

The extra condition:
&& !(is(T == struct) && is(typeof(&T.init.toString))) )

And the toString exception needs to added to Line 1474
void formatValue(Writer, T, Char)(Writer w, T val,
        ref FormatSpec!Char f)
if (is(T == struct) && (!isInputRange!T  || is(typeof(&T.init.toString))) )

By the way, I like the idea of
   void toString(void delegate(const(char)[]) sink, FormatSpec fmt);
   void toString(void delegate(const(char)[]) sink, string fmt);
but are which one is preferred? And if/when is Object going to switch over?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 14, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6231


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

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


--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> 2012-06-14 02:38:46 PDT ---
Now almost bugs around formatting were fixed.

(In reply to comment #1)
> By the way, I like the idea of
>    void toString(void delegate(const(char)[]) sink, FormatSpec fmt);
>    void toString(void delegate(const(char)[]) sink, string fmt);
> but are which one is preferred? And if/when is Object going to switch over?

Getting FormatSpec version is most specialized than others, so is preferred. And such enhanced toStrings are priority than Object.toString() in class.

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