Why does toString() exist? It seems useless.

Leandro Lucarella llucax at gmail.com
Fri Aug 22 13:10:21 PDT 2008


Sean Kelly, el 22 de agosto a las 12:46 me escribiste:
> Leandro Lucarella wrote:
> >Don, el 18 de agosto a las 12:41 me escribiste:
> >>I cannot understand the rationale for a toString() member function which 
> >>doesn't support formatting.
> >I think is useful only for debugging purposes.
> >>C++ got around this by giving state to the iostream classes.
> >I think C++ formatting is heavily ill. All the manipulator stuff is
> >madness.
> 
> It's horrific but surprisingly flexible.  I've created stateful formatters for 
> various protocols and made it work all invisibly with the formatting hooks 
> provided in C++.  For example:
> 
>    std::cout << a << b << c << std::flush;
> 
> The above may perform lazy output of structured XML, some binary encoding, etc.  
> It might be possible to get close with toString, but because it returns an array 
> rather than writing into a buffer some of the flexibiliy (like lazy encoding) 
> would definitely be lost.
> 
> My experience with Java suggests that toString is meant for debugging purposes 
> anyway.  It's rare that an object will produce meaningful user-level output with 
> its toString method.

I agree, I meant that D should do better. Like templates are for gurus in
C++ and for people in D, formatting should be for people too in D (as
opposed to C++ manipulators =).

-- 
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
----------------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------------
He cometido pecados, he hecho el mal, he sido víctima de la envidia, el
egoísmo, la ambición, la mentira y la frivolidad, pero siempre he sido
un padre argentino que quiere que su hijo triunfe en la vida.
	-- Ricardo Vaporeso


More information about the Digitalmars-d-learn mailing list