toString, to!(char[]) & co
Fawzi Mohamed
fawzi at gmx.ch
Wed Mar 10 15:17:29 PST 2010
On 10-mar-10, at 23:52, bearophile wrote:
> Fawzi Mohamed:
>
>> If you look at the allocations needed for a toString, one sees the
>> allocation of a lot of small strings, alloacation for the
>> concatenation
>> of a couple of the small strings, then again for the larger blocks
>> until you get the final string.
>
> A possible idea is to replace the current object.toString:
> string toString();
>
> With something vaguely like (keeping it simple):
> string toString(void delegate(string) sink=null);
>
> If called with no arguments the toString has to output a string, as
> now.
>
> If called with a sink that is not null, the toString can choose to
> use it for the output and return an empty string, or ignore it and
> return a string.
>
> The caller can give a sink or not. But even if gives a sink it has
> to test if the return value is an nonempty string, because the
> toString is free to ignore the given sink.
>
> [Extra: if toString both returns a nonempty string and gives
> something to the sink, then the caller can use a nice silver coin to
> choose among the two.]
this makes the implementation of toString more complicated, I have
void desc(void delegate(string))
and actually normally I mixin serialization and then mixin a template
(printOut) that implements desc using Json serialization, and toString
by collecting desc into a GrowableArray...
More information about the Digitalmars-d
mailing list