dmd 2.029 release
Frits van Bommel
fvbommel at REMwOVExCAPSs.nl
Thu Apr 23 06:35:59 PDT 2009
Denis Koroskin wrote:
> On Thu, 23 Apr 2009 16:20:03 +0400, Don <nospam at nospam.com> wrote:
>
>> 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.
>>
>
> Absolutely agree, but Sink is not good, either. I have previously suggested the following design (but can't find my post anymore):
>
> A signature of toString() should be as follows:
>
> char[] toString(string format = null, char[] buffer = null);
>
> Bonuses you get from that:
> You don't have to change your code (aside from toString() returning mutable array now, but it is very easy to fix)
> It allows you to avoid allocations - just pass a temporary buffer to use!
It'll still allocate if the buffer isn't big enough.
I usually define something like "void streamTo(Sink sink)" in a base class if I
want non-allocating output. Adding a format string to the parameter list should
be easy, but I haven't needed it yet.
I then usually implement toString by passing an appending Sink to that method,
just so Tango's formatting methods will be able to use it.
IMHO It'd be pretty nice for the standard formatting systems (both the Tango and
Phobos ones) to just call a standard Object method taking (Sink sink, char[]
format = null) on objects.
Backward compatibility might be tricky though, if you want to support both
overriding and calling of toString(). (You can't have the default toString
implementation call the format function *and* have the default format function
implementation call toString)
Though I suppose you could take a delegate to toString in the default format
method and see if the function pointer in it points to Object.toString, just
calling sink(this.classinfo.name) if so (to emulate current behavior), and
calling toString() if not...
(Or maybe the other way around; have Object.toString check if the format
function was overridden, call it if so and return the classname if not)
More information about the Digitalmars-d-announce
mailing list