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?
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?
Andrei
I'd like to point out that parsing a format string for every object/variable is
very inefficient. I'd recommend having the virtual writeTo function accept
FormatSpec, like the formatValue routines, and then make the writeTo which
takes a format string be final. i.e.:
void writeTo(void delegate(const(char)[]) sink, ref FormatSpec!(Char) format);
void writeTo(void delegate(const(char)[]) sink, string format = null) final {
auto spec = FormatSpec!char(format);
writeTo(sink, spec);
}