On Sun, 04 Sep 2011 00:06:47 -0400, Andrei Alexandrescu
<seewebsiteforem...@erdani.org> wrote:
On 8/30/11 8:59 PM, Paul D. Anderson wrote:
Can someone clarify for me the status and/or direction of string
formatting in D?
[snip]
I agree there are major inefficiencies and composability problems caused
by a blind toString() that creates a whole new string without any
assistance. So we need to fix that.
There are suggestions to add this method to Object:
void writeTo(void delegate(const(char)[]) sink, string format = null);
Then, the suggestion goes, whether or not we deprecate toString, in the
short term it should be implemented in terms of writeTo.
There are a few questions raised by this proposal:
1. Okay, this takes care of streaming text. How about streaming in
binary format?
toString is a means of communicating the state of an object to a person
reading a screen. That's it. It's not meant to be a serializer function.
So I guess the answer is, because people cannot read binary (well, some
can, but that's just showing off).
2. Since we have a relatively involved "output to text" routine, how
about an "input from text" routine? If writeTo is there, where is
readFrom?
It's a good point. But there is no current means to do this. AFAIK,
readf only works on primitives, right? Note that the DIP was born out of
frustration with the inefficiency of toString. There was no frustration
at the inefficiency of, um.. parseString? because it didn't exist.
I don't feel that lack of readFrom necessarily precludes writeTo, because
printing objects for debugging is a well-used and frequently needed
thing. However, parsing objects from text is not as frequently needed or
common. But I also feel that a proposal for readFrom is not precluded by
DIP9, and in fact, it's probably very logical to derive such a proposal
from DIP9. I think they can be implemented separately.
If I could be so bold as to suggest tying in with my recently revealed
stdio overhaul:
size_t readFrom(const(char)[] data, size_t start); // same as readUntil
delegate
readf calls (with a possible translation to char[] data):
input.readUntil(&obj.readFrom);
-Steve