std.format range with compound format specifiers?

Petar Petar
Wed Nov 20 00:28:17 UTC 2019


On Tuesday, 19 November 2019 at 21:50:08 UTC, Steven 
Schveighoffer wrote:
> I know I can format a range with a format string that contains 
> %(%s, %). And this results in a nice comma separated list for 
> each item.
>
> But what about an item that has a not-so-cookie-cutter format? 
> Like for instance a name/value field:
>
> struct NV
> {
>   string name;
>   int value;
> }
>
> If I want to print one of these, I can do:
>
> format("%s: %s", nv.name, nv.value);
>
> If I wanted to print a range of these, let's say:
>
> auto arr = [NV("Steve", 1), NV("George", 500), NV("Adam", -5)];
>
> How can I have it come out like:
>
> Steve: 1, George: 500, Adam: -5
>
> Do I have to define a toString method in the NV struct? Is 
> there not another way besides doing this?
>
> -Steve

In cases where I have some aggregate data, but I don't feel like 
writing a custom toString method, I often wrap the data in a 
Tuple and use its [1] %(inner%) or %(inner%|sep%) format 
specifiers. Here's an example:

import std;
void main()
{
     {
         alias NV = tuple;
         auto arr = [NV("Steve", 1), NV("George", 500), NV("Adam", 
-5)];
         writefln("%(%(%s: %s%), %)", arr);
     }

     {
         static struct NV
         {
             string name;
             int value;
         }
         auto arr = [NV("Steve", 1), NV("George", 500), NV("Adam", 
-5)];
         writefln("%(%(%s: %s%), %)", arr.map!(obj => 
obj.tupleof.tuple));
     }
}

In this case, from outside to inside, I am first formatting the 
range and then for each tuple I am formatting its fields one by 
one.

If for exmaple I want to format a tuple with 3 double, each one 
of them with a different number of digits after the decimal 
point, I could do:
"%(%.1f %.2f %.3f%)".writefln(tuple(1.5, 1.25, 1.125));

If on the other hand I want to format all tuple elements the 
same, I would use this scheme:
"%(%.1f%| %)".writefln(tuple(1.5, 1.25, 1.125));

I think we should extend std.format with support for using the 
same tuple formatting specifier as std.typecons.Tuple, but for 
structs and possibly classes, as I find it quite useful.

[1]: https://dlang.org/phobos/std_typecons#.Tuple.toString


More information about the Digitalmars-d-learn mailing list