Don wrote:
No!
<rant>
toString() is one of the most dreadful features in D. Trying to slightly
improve it is a waste of time -- the whole concept needs to be redone.
It's horribly inflexible, tedious, and hugely inefficient. What more
could there be to hate?
I agree.
- the object being called has no context. It doesn't know what format is
desired, for example.
- you can't emulate formatting of built-in types, NOT EVEN int! You
can't do left-align, pad with zeros, include + sign, display in hex.
- it's got no stream concept. Every object has to create and manage its
own buffer, and nobody knows if anyone actually needs it.
Yah. std.stdio and std.format already uses streaming internally to some
extent. I plan to make it entirely possible to dump a string and a
binary representation of an object to a generic stream, without having
to allocate any memory in the process.
It ought to be at least as simple as:
struct Foo(A, B, C){
A[10] a;
B b;
C c;
void toString(Sink sink){
foreach(x; a) sink(x);
sink(b);
sink(c);
}
}
... but it's not, you have to create a silly buffer to put all your
strings in, even if there are 200 million of them and your giant string
is just going to be written to a file anyway.
Yes. The way it should be is not with sink, but with the standard output
iterator method put().
void streamOut(T, R)(T object, R range)
{
foreach(x; a) range.put(x);
range.put(b);
range.put(c);
}
I'd like to see version(debug) {} put around Object.toString(). It's a
deathtrap feature that's got no business being used other than for
debugging.
Well toString is ok for casual uses, but I agree that true streaming
mops the floor with it.
Andrei