std.format range with compound format specifiers?

Steven Schveighoffer schveiguy at gmail.com
Tue Nov 19 21:50:08 UTC 2019


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


More information about the Digitalmars-d-learn mailing list