How to get a range from std.container.array for use with std.format.sformat?

Seb seb at wilzba.ch
Tue Jan 30 19:20:29 UTC 2018


On Tuesday, 30 January 2018 at 18:42:45 UTC, Steven Schveighoffer 
wrote:
> On 1/30/18 12:53 PM, cc wrote:
>>      import std.container;
>>      import std.format;
>>      Array!char str;
>>      str.length = 256;
>> 
>>      str.sformat!"%s:%s"("some", "string");
>>      // Error: template std.format.sformat cannot deduce 
>> function from argument types !("%s:%s")(Array!char, string, 
>> string), candidates are:
>>      //   std.format.sformat(alias fmt, Args...)(char[] buf, 
>> Args args) if (isSomeString!(typeof(fmt)))
>>      //   std.format.sformat(Char, Args...)(char[] buf, in 
>> Char[] fmt, Args args)
>
> sformat requires a builtin array, apparently. It doesn't work 
> for other types.

For everything else, you can use formattedWrite:


```
import std.array : appender;
auto writer = appender!string();
writer.formattedWrite!"%s is the ultimate %s."(42, "answer");
writeln(writer.data);
```

without any GC:

```
/+dub.sdl:
dependency "emsi_containers" version="~>0.6.0"
+/
import std.format, std.stdio;

void main()
{
     import containers;
     DynamicArray!char arr;
     arr.formattedWrite!"%s is the ultimate %s."(42, "answer");
     printf("%.*s\n", arr.length, arr.ptr);
}
```

https://run.dlang.io/is/Taem9j


More information about the Digitalmars-d-learn mailing list