Efficient outputting of to-string conversions

Steven Schveighoffer schveiguy at yahoo.com
Tue Feb 8 08:09:44 PST 2011


On Tue, 08 Feb 2011 10:55:23 -0500, spir <denis.spir at gmail.com> wrote:

> On 02/07/2011 11:38 PM, Tomek Sowiński wrote:
>> Jonathan M Davis napisał:
>>
>>> On Monday 07 February 2011 13:10:09 Tomek Sowiński wrote:
>>>> Looks like std.conv.to always allocates behind the scenes. It's a  
>>>> shame as
>>>> the returned string is immediately processed and discarded in my XML
>>>> writer. Are there plans to include a custom output variant, e.g.
>>>> to!string(7, outputRange)?
>>>
>>> http://prowiki.org/wiki4d/wiki.cgi?LanguageDevel/DIPs/DIP9
>>
>> I know about Steven's proposal but it applies only to user types not  
>> primitives. Either way std.conv.to would need a buffered output range  
>> as integers are written from the right. Any chance for an abstraction  
>> analogous to buffered input ranges discussed recently?
>
> I haven't read a solution like the proposal for writeTo would not apply  
> to primitives. Is it really so? (If only for language consistency, I  
> would prefere this big shift to apply to all types.)

writeTo does not need to apply to primitives because primitives can be  
written to streams without first converting to char[].  For instance, one  
does not write:

writeln(to!string(5))

The whole point of writeTo is to allow easy output of custom data types  
that the standard library does not know how to output.  It was not meant  
to extend to primitives.

As far as Tomek's request, I would expect  
std.format.formattedWrite(buffer, "%s", 7) to work, or  
std.format.formatValue (with associated FormatSpec, which I don't feel  
like looking up the usage of)

-Steve


More information about the Digitalmars-d mailing list