www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2287] New: to!(string)(struct) should work if struct has toString() function

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

           Summary: to!(string)(struct) should work if struct has toString()
                    function
           Product: D
           Version: 2.018
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: dsimcha yahoo.com


import std.stdio, std.conv;

struct Foo {
    string toString() {
        return "test";
    }
}

void main() {
    Foo test;
    //Doesn't work.  std.conv unaware of foo.toString().
    writefln(to!(string)(test));
}

Obviously, this is a very simplified test case, but when writing generic code,
it can become a legitimate issue.  Also note that the following test case,
which just changes Foo to a class, actually works.

import std.stdio, std.conv;

class Foo {

    this(){}

    string toString() {
        return "test";
    }
}

void main() {
    Foo test = new Foo;
    writefln(to!(string)(test));
}

I've found the bug and the fix is a dead simple two-liner.  I'll attach the
diffs against conv.d.


-- 
Aug 16 2008
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2287





------- Comment #1 from dsimcha yahoo.com  2008-08-16 18:52 -------
Created an attachment (id=270)
 --> (http://d.puremagic.com/issues/attachment.cgi?id=270&action=view)
Call toString() if available.


-- 
Aug 16 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2287





------- Comment #2 from dsimcha yahoo.com  2008-08-16 19:53 -------
Please ignore the patch I posted.  Upon further testing, it seems to break
other stuff.


-- 
Aug 16 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2287


andrei metalanguage.com changed:

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




------- Comment #3 from andrei metalanguage.com  2008-08-16 23:23 -------
Added the appropriate code. Hopefully that doesn't break anything else :o).
Committed, will be distributed with next release.


-- 
Aug 16 2008
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2287





------- Comment #4 from bugzilla digitalmars.com  2008-09-03 01:40 -------
Fixed dmd 2.019


-- 
Sep 02 2008