Unexpected issue with std.format

Saurabh Das saurabh.das at gmail.com
Sat Feb 1 13:39:34 UTC 2020


I faced this issue while working with custom formatting for a 
struct. I have reduced the error down to this test program:

import std.format, std.stdio, std.array;

struct Test1
{
     void toString(W, C)(ref W w, scope const ref FormatSpec!C fmt)
     {
         pragma(msg, "Test1 function compiled with W=" ~ 
W.stringof);
         // formatValue(w, this, fmt);
     }
}

struct Test2
{
     void toString(W, C)(ref W w, scope const ref FormatSpec!C fmt)
     {
         pragma(msg, "Test2 function compiled with W=" ~ 
W.stringof);
         formatValue(w, this, fmt);
     }
}

void main()
{
     Test1 t1;
     Test2 t2;

     Appender!string writer;
     auto ff = singleSpec("%s");

     formatValue(writer, t1, ff);
     formatValue(writer, t2, ff);
}

When compiled, the output is:

Test1 function compiled with W=S
Test1 function compiled with W=Appender!string
Test2 function compiled with W=S

1. Why was Test2 never compiled with W=Appender!string?
2. What is "S"?

Essentially, my custom struct was not being formatted using the 
toString method that I had written. Reducing the issue, it seems 
like a call to formatValue with the same type caused the issue. 
If someone can explain what I am doing wrong here, it would 
really help a lot.

Thanks,
Saurabh



More information about the Digitalmars-d-learn mailing list