std.string.format call from varyargs

Nicholas Wilson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jul 1 19:55:26 PDT 2017


On Sunday, 2 July 2017 at 00:49:30 UTC, LeqxLeqx wrote:
> Hello!
>
> How does one go about invoking a templated-variatic function 
> such as std.string.format
> with an array of objects?
>
> For example:
>
> string stringMyThing (string formatForMyThings, MyThing[] 
> myThings)
> {
>   return format(
>     formatForMyThings,
>     myThings
>     );
> }
>
>
>
> In executing the above, the 'format' method always interprets 
> the entire array 'myThings' as the first argument to the 
> format, and no arguments afterwards, resulting in 'orphaned' 
> format specifiers if the array is longer than a single element. 
> Even if the array is only a single element, the formatter will 
> wrap the result of the element's 'toString()' method with '[' 
> and ']'
>
> I really don't want to write my own format parser.
> Any help would be much appreciated!
>
>
> Thanks!

string stringMyThing (string fmt, MyThing[]  myThings)
{
     Appender!(string) s;
     foreach(thing; myThings);
         s.append(format(fmt,thing));
     return s.data();
  }

sorry I can't remember the appender api properly.


More information about the Digitalmars-d-learn mailing list