toString, to!(char[]) & co

bearophile bearophileHUGS at lycos.com
Wed Mar 10 14:52:19 PST 2010


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.]

Bye,
bearophile



More information about the Digitalmars-d mailing list