www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5042] New: format("%s") of struct without toString

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


--- Comment #0 from bearophile_hugs 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: -------
Oct 11 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5042


Andrei Alexandrescu <andrei metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |andrei metalanguage.com
         AssignedTo|nobody puremagic.com        |andrei metalanguage.com


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 09 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5042


SomeDude <lovelydear mailmetrash.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lovelydear mailmetrash.com


--- Comment #1 from SomeDude <lovelydear mailmetrash.com> 2012-04-22 09:13:12
PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 22 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5042


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |DUPLICATE


--- Comment #2 from Kenji Hara <k.hara.pg gmail.com> 2012-04-22 17:36:58 PDT ---
*** This issue has been marked as a duplicate of issue 6595 ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 22 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5042



--- Comment #3 from bearophile_hugs eml.cc 2012-04-24 18:42:50 PDT ---
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...

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 24 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5042



--- Comment #4 from Kenji Hara <k.hara.pg gmail.com> 2012-04-24 18:56:37 PDT ---
(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. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 24 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5042



--- Comment #5 from bearophile_hugs eml.cc 2012-04-24 19:07:34 PDT ---
(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) } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 24 2012