On Saturday, 2 May 2020 at 10:36:47 UTC, Ali Çehreli wrote:
On 5/1/20 7:40 PM, dan wrote:> On Saturday, 2 May 2020 at 02:29:43 UTC, H. S. Teoh wrote:
>> On Sat, May 02, 2020 at 02:22:42AM +0000, dan via
Digitalmars-d-learn
>> wrote:
>>> I'm looking for a function something like writeln or write,
but
>>> instead of writing to stdout, it writes to a string and
returns the
>>> string.
>> [...]
>>
>>     import std.format : format;
>>     string str = format("%s %s %s", obj1, obj2, obj3);
>>
>>
>> T
>
> Thanks HS!
>
> That looks like a good move, if format will do the string
conversion for
> me.
>
> But one thing that would be troublesome is that i would have
to make
> sure to count up the %s so that they match the number of
arguments.  I
> would like to do without that, just like writeln does.

If you can live with a mildly awkward way of passing it, format() can take the format string at compile time as well:

  string str = format!"%s %s %s"(obj1, obj2, obj3);

You get a compilation error if format specifications don't match the arguments. (There are bug reports about that check but it mostly works great.)

Ali

Thanks Ali.

That's also a good point, and would remove one of my qualms about all of the %s reps.

So if for any reason i cannot use the text function (or if i want to double check on the types of the objects) this would be a good thing to use.

dan

Reply via email to