toString refactor in druntime

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Mon Oct 27 12:20:24 PDT 2014


On 10/27/14 2:45 PM, Daniel Murphy wrote:
> "Benjamin Thaut"  wrote in message news:m2m3j2$ciu$1 at digitalmars.com...
>
>> They wouldn't get any uglier than they already are, because the
>> current toString functions within druntime also can't use std.format.
>>
>> An example would be to toString function of TypInfo_StaticArray:
>>
>> override string toString() const
>> {
>> SizeStringBuff tmpBuff = void;
>> return value.toString() ~ "[" ~
>> cast(string)len.sizeToTempString(tmpBuff) ~ "]";
>> }
>>
>> Would be replaced by:
>>
>> override void toString(void delegate(const(char)[]) sink) const
>> {
>>   SizeStringBuff tmpBuff = void;
>> value.toString(sink);
>> sink("[");
>> sink(cast(string)len.sizeToTempString(tmpBuff));
>> sink("]");
>> }
>>
>> The advantage would be that the new version now ideally never
>> allocates. While the old version allocated 3 times of which 2
>> allocations end up beeing garbage right away.
>>
>> Also I rember reading that the long term goal is to convert all
>> toString functions to the sink version.
>
> It's very ugly compared to the formattedWrite version, and it does
> increase the line count compared to the current version (this is the
> main disadvantage of the sink-based toString IMO).  If this is as bad as
> it gets, PR approval shouldn't be a problem.

Might I suggest a helper that takes a sink and a variadic list of 
strings, and outputs those strings to the sink in order.

-Steve


More information about the Digitalmars-d mailing list