Timon Gehr <timon.g...@gmx.ch> wrote:
> On 09/01/2011 09:41 PM, Don wrote:
>> On 31.08.2011 14:35, Timon Gehr wrote:
>>> On 08/31/2011 04:41 AM, Jonathan M Davis wrote:
>>>> Objects would have writeTo and toString would presumably be
>>>> deprecated.
>>>> 
>>> 
>>> I have never understood the rationale behind deprecating toString once
>>> we have writeTo. Why should it be deprecated?
>> 
>> Code bloat. Every struct contains string toString().
>> Quite unnecessarily, since it can always be synthesized from the more
>> complete version.
> 
> I was just suggesting to keep the existing support for toString() inside
> to, format etc. Of course, all the structs in Phobos should probably
> completely migrate to writeTo.
> 
>> 
>> toString is great in case
>>> you just want to quickly and easily convert something to a string, and
>>> later, if formatting or more efficient output etc. is needed, the method
>>> can transparently be replaced by writeTo.
>> 
>> BTW, you do realize that code using writeTo is shorter in most cases?
>> The reason is, that it can omit all the calls to format().
>> Pretty much the only time when toString is simpler, is when it is a
>> single call to format().
>> It's only really the signature which is more complicated.
>> 
> 
> I am not convinced:
> 
> struct S{
>     int x,y,z;
>     void writeTo(void delegate(const(char)[]) sink, string format = null){
>         sink("(");
>         .writeTo(x,sink,"d"); // still no UFCS
>         sink(", ");
>         .writeTo(y,sink,"d");
>         sink(", ");
>         .writeTo(z,sink,"d");
>         sink(")");
>     }
> 
>     string toString(){return "("~join(map!(to!string)([x,y,z]),", ")~")";}
> }

to!string of array can support multiple arguments:

string toString() {
    return to!string([x, y, z], "(", ", ", ")");
}

and I believe writeTo could be made to accept extra arguments too:

struct S {
void writeTo(SomeType sink, const char[] format = null) {
    [x, y, z].writeTo(sink, format, "(", ", ", ")");
}
...
}

void writeTo(T)(T[] arr, SomeType sink, const char[] format = null, const
char[] open = "[", etc) {
  ...
}

Reply via email to