Semantics of toString

Bill Baxter wbaxter at gmail.com
Tue Nov 10 06:52:38 PST 2009


On Tue, Nov 10, 2009 at 4:40 AM, Don <nospam at nospam.com> wrote:
> Lutger wrote:
>>
>> Don wrote:
>> ...
>>>
>>> There is a definite use for such as thing. But the existing toString()
>>> is much, much worse than useless. People think you can do something with
>>> it, but you can't.
>>> eg, people have asked for BigInt to support toString(). That is an
>>> over-my-dead-body.
>>
>> Since you are in the know and probably the biggest toString() hater
>> around: are there plans (or rejections thereof) to change toString() before
>> D2 turns gold? Seems to me it could break quite some code.
>
>
> I'm hoping someone will come up with a design.
>
> Straw man:
>
> void toString(void delegate(const(char)[]) sink, string fmt) {
>
> // fmt holds the format string from writefln/formatln.
> // call sink() to print partial results.
>
> }

That looks pretty good, actually.
I guess I would like to see plain no-arg toString() still supported.

A default toString() could be implemented in terms of the fancy one as:

string toString() {
     char buf[];
     toString( (string s) { buf ~= s; }, "" );
     return assumeUnique!(buf);
}

could be a mixin in a library I suppose.

I think I would like to see the format strings not necessarily tied to
writefln's particular format.

--bb



More information about the Digitalmars-d mailing list