On 01.09.2011 23:35, Timon Gehr wrote:
On 09/01/2011 11:15 PM, kennytm wrote:
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], "(", ", ", ")");
}

This runs in ~66% of the time of Steve's formattedWrite solution.
(if the delegate just appends to some string variable)



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) {
...
}

ok, getting better. But still, I think to!string should remain to be
able to use toString if available. (in this case, a 33% speed advantage!)

If you're concerned about speed, the writeTo method is much quicker, since it doesn't require any heap activity at all.

Reply via email to