www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6231] New: [patch] std.conv.to: Structs with toString and isInputRange match multiple templates.

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



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: -------
Jun 30 2011
next sibling parent d-bugmail puremagic.com writes:
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.



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: -------
Jun 30 2011
prev sibling parent d-bugmail puremagic.com writes:
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



Now almost bugs around formatting were fixed.


 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: -------
Jun 14 2012