How to forward format specifiers ?
Element 126 via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Jun 29 09:08:24 PDT 2014
On 06/29/2014 04:22 PM, Ali Çehreli wrote:
> 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
>
Thanks a lot ! I just checked if it also worked for structs and classes
and it does the job perfectly.
I have used formattedWrite for months without noticing formatValue, even
though it was on the wiki.
Maybe I should add an example to the documentation of std.format.
formatValue is present but without any example, compared to the
extensive documentation of formattedWrite.
More information about the Digitalmars-d-learn
mailing list