Output range of ranges to single buffer

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jan 14 09:04:19 PST 2016


On Thursday, January 14, 2016 08:41:23 Jacob Carlborg via Digitalmars-d-learn wrote:
> On 2016-01-13 22:20, H. S. Teoh via Digitalmars-d-learn wrote:
>
> > Isn't that just a matter of replacing each of the segments with their
> > range equivalents? Also, std.format.formattedWrite will do
> > writeln-formatting into a buffer (well, any output range, really) -- I'm
> > pretty sure it doesn't allocate, at least for the simplest cases like
> > converting an integer. So you should be able to do something like this:
> >
> >     auto data = [ 1, 2, 3, 4, 5 ];
> >     char[] buf = ...;
> >     formattedWrite(buf, "[%(%d, %)]", data);
>
> Aha, interesting. I didn't know formattedWrite could format an
> array/range directly like that.
>
> The more complex example works by defining "toString" which takes an
> output range (delegate). But what if I need to format a third party type
> that I cannot add methods to? UFCS does not seem to work.

You can't do toString via UFCS any more than you can overload any operators
via UFCS. If a type's toString does not work how you want, or a type does
not provide one, then you'll need to convert objects of that type to a
string in a different way.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list