Bug 5042 – format("%s") of struct without toString

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2010-10-11T15:18:00Z
Last change time
2012-04-24T19:07:34Z
Keywords
rejects-valid
Assigned to
andrei
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2010-10-11T15:18:36Z
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)").
Comment #1 by lovelydear — 2012-04-22T09:13:12Z
I don't know if the bug is that std.string.format doesn't work on everything or if it's the sketchy documentation, "Format arguments into a string.", which implies that it should work on everything without further explanations.
Comment #2 by k.hara.pg — 2012-04-22T17:36:58Z
*** This issue has been marked as a duplicate of issue 6595 ***
Comment #3 by bearophile_hugs — 2012-04-24T18:42:50Z
In dmd 2.060alpha: import std.stdio: writeln, writefln; import std.string: format; struct Foo { int x; } void main() { writeln(Foo(1)); // ==> Foo(1) writefln("%s", Foo(1)); // ==> Foo(1) writeln(format("%s", Foo(1))); // run-time error } I see this error still: Foo(1) Foo(1) std.format.FormatException@std\format.d(4749): Can't convert test.Foo to string: "string toString()" not defined If it's isn't a problem in just my DMD, then I'll reopen this issue...
Comment #4 by k.hara.pg — 2012-04-24T18:56:37Z
(In reply to comment #3) > In dmd 2.060alpha: > [snip] > > If it's isn't a problem in just my DMD, then I'll reopen this issue... std.string.format function only support TypeInfo based formatting. Then it never supports raw field based, and alias this based formatting. As discussed here: https://github.com/D-Programming-Language/phobos/pull/231 For the backward compatibility, we cannot change the implementation of std.string.format function. Instead, in 2.060head, we add a xformat function that uses std.format.formattedWrite function as the implementation. After all, you should use std.string.xformat instead of std.string.format in 2.060 and later.
Comment #5 by bearophile_hugs — 2012-04-24T19:07:34Z
(In reply to comment #4) > After all, you should use std.string.xformat instead of std.string.format in > 2.060 and later. Thank you Hara, this works: import std.stdio: writeln, writefln; import std.string: xformat; struct Foo { int x; } void main() { writeln(Foo(1)); // ==> Foo(1) writefln("%s", Foo(1)); // ==> Foo(1) writeln(xformat("%s", Foo(1))); // ==> Foo(1) }