www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - ? toString(?)

reply simendsjo <simen.endsjo pandavre.com> writes:
Object contains toString as the following:
string toString();

But there are also other versions going about:
void toString(void delegate(const(char)[]) sink, string fmt);
void toString(void delegate(const(char)[]) sink, FormatSpec fmt);

I wouldn't be surprised to see some range based version too.
Where are the different versions required, and what is best practice 
when implementing toString?
Jun 09 2011
parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On 2011-06-09 08:38, simendsjo wrote:
 Object contains toString as the following:
 string toString();
 
 But there are also other versions going about:
 void toString(void delegate(const(char)[]) sink, string fmt);
 void toString(void delegate(const(char)[]) sink, FormatSpec fmt);
 
 I wouldn't be surprised to see some range based version too.
 Where are the different versions required, and what is best practice
 when implementing toString?
At this point, I believe that the typical thing is to just implement string toString(); There's a proposal for a replacement for toString (writeTo IIRC) which would write to a buffer (which was a scoped delegate IIRC), and then we'd be using to!string() (which would call writeTo) instead of toString() and toString would go away. But that proposal hasn't gone through yet (though I expect it to eventually). If Object has those overloads for toString, then you can overload them if you want to, but I'm not quite sure what uses them at this point. I think that most people just overload the normal toString. There are some people though (such as Don) who really don't like how that creates a string every time that you call it, so you can't reuse a buffer. I expect that the delegate versions of toString are precursors to the writeTo proposal. - Jonathan M Davis
Jun 09 2011