Bug 2287 – to!(string)(struct) should work if struct has toString() function

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2008-08-16T18:45:00Z
Last change time
2015-06-09T01:20:08Z
Assigned to
bugzilla
Creator
dsimcha

Attachments

IDFilenameSummaryContent-TypeSize
270conv.diffCall toString() if available.text/plain148

Comments

Comment #0 by dsimcha — 2008-08-16T18:45:24Z
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.
Comment #1 by dsimcha — 2008-08-16T18:52:07Z
Created attachment 270 Call toString() if available.
Comment #2 by dsimcha — 2008-08-16T19:53:18Z
Please ignore the patch I posted. Upon further testing, it seems to break other stuff.
Comment #3 by andrei — 2008-08-16T23:23:12Z
Added the appropriate code. Hopefully that doesn't break anything else :o). Committed, will be distributed with next release.
Comment #4 by bugzilla — 2008-09-03T01:40:50Z
Fixed dmd 2.019