[Issue 5042] New: format("%s") of struct without toString
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon Oct 11 15:19:06 PDT 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5042
Summary: format("%s") of struct without toString
Product: D
Version: D2
Platform: x86
OS/Version: Windows
Status: NEW
Keywords: rejects-valid
Severity: normal
Priority: P2
Component: Phobos
AssignedTo: nobody at puremagic.com
ReportedBy: bearophile_hugs at eml.cc
--- Comment #0 from bearophile_hugs at eml.cc 2010-10-11 15:18:36 PDT ---
Given a plain struct like Foo, the to!string creates a good enough textual
representation of it:
import std.conv: to;
struct Foo { int x; }
void main() {
assert(to!string(Foo(1)) == "Foo(1)");
}
A nomal writefln("%s") just prints "Foo":
import std.stdio: writeln, writefln;
struct Foo { int x; }
void main() {
writeln(Foo(1)); // ==> Foo
writefln("%s", Foo(1)); // ==> Foo
}
But the format("%s") doesn't work:
import std.string: format;
struct Foo { int x; }
void main() {
assert(format("%s", Foo(1)) == "Foo(1)");
}
DMD 2.049 gives the error:
std.format.FormatError: std.format Can't convert test.Point to string: "string
toString()" not defined
I expect this format("%s") to return "Foo(1)" or "Foo" (I prefer "Foo(1)").
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list