How to forward format specifiers ?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 29 07:22:46 PDT 2014


On 06/29/2014 04:55 AM, Element 126 wrote:

 > I've certainly missed something

formatValue passes your tests:

import std.stdio;
import std.format: formattedWrite, FormatSpec, formatValue;
import std.string: format;

struct wrapper(T) {

     private T val;

     public this(T newVal) pure { val = newVal; }

     public void toString(
         scope void delegate(const(char)[]) sink,
         FormatSpec!char fmt
     ) const
     {
         formatValue(sink, val, fmt);    // <-- HERE
     }
}

unittest {

     immutable uint base = 16;
     auto a = wrapper!uint(base);
     assert(format("%x", a) == format("%x", base));
     assert(format("%08x", a) == format("%08x", base));
}

void main()
{}

Ali



More information about the Digitalmars-d-learn mailing list