Why is there no lazy `format`?

Steven Schveighoffer schveiguy at gmail.com
Tue Oct 20 17:10:12 UTC 2020


On 10/20/20 9:28 AM, burt wrote:
> 
> The range returned by `formatRange` could have an internal buffer of 
> maybe 16 characters that stores small strings, e.g. for small integers. 
> It would also allow chaining with other range algorithms: you would call 
> `.joiner()` on it to get an input range of chars.
> 
> Is this something worth including in the standard library (presumably in 
> std.format)?
> 
> (The same may also be possible for `std.conv.text` but I did not look 
> into this.)

I think it's possible, but also it needs a buffer. Which means it needs 
to allocate. Even a 16 character buffer might not be enough.

std.format is not designed around tracking an in-progress conversion, so 
you would have to convert whole things at once. It might not be that 
desirable.

For example:

formatRange("%s", someLargeArrayOrStruct);

this is going to have to buffer the *whole thing*, and then give you 
lazy access to the buffer.

In order for this to work, I think you would have to redesign how format 
works. It's not an easy thing, but could be an interesting way of 
looking at it.

Note that you can probably mimic this with fibers, but that's really 
heavy for this task. And you still need to allocate a buffer.

-Steve


More information about the Digitalmars-d mailing list