[Issue 7881] New: std.string.format does not support structs with no toString

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Apr 9 16:01:36 PDT 2012


http://d.puremagic.com/issues/show_bug.cgi?id=7881

           Summary: std.string.format does not support structs with no
                    toString
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: andrej.mitrovich at gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2012-04-09 16:02:18 PDT ---
import std.string;
import std.stdio;

struct Foo
{
    string name;
}

void main()
{
    Foo foo;

    // ok
    writefln("%s", foo);  

    // Can't convert test.Foo to string: "string toString()" not defined
    format("%s", foo);  
}

I thought I was being clever when implementing this:
void printfln(string file = __FILE__, int line = __LINE__, T...)(T t)
{
    writefln("%s L %s - %s", file, line, format(t[0], t[1 .. $]));
}

It gives me a nice file+line when printing (great for debugging). Unfortunately
format() fails to work in numerous cases that writef has no issues with.

Anywho I can use this workaround:
void printfln(string file = __FILE__, int line = __LINE__, T...)(T t)
{
    writef("%s L %s - ", file, line);
    writefln(t[0], t[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