Why does toString() exist? It seems useless.

Sean Kelly sean at invisibleduck.org
Fri Aug 22 15:28:19 PDT 2008


Fawzi Mohamed wrote:
> On 2008-08-22 22:10:21 +0200, Leandro Lucarella <llucax at gmail.com> said:
> 
>> 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;
> 
> I think that putting formatting details in the stream is a very good 
> idea for serialization, but not for normal textual output, the two 
> things are not the same (even if it might be useful to use some xml 
> serialization for some objects, and send it together with "normal" text).

Yeah, the above example was a simplified version of some pretty fancy 
stuff I did for a multi-protocol server app.  It made serialization 
painfully easy for the user (so long as the proper support code was 
built into the formatting frastructure), but was really operating well 
beyond the scope of what iostream formatting was intended to do.

In general though I think it's preferable to have such things done 
differently than raw IO.  Tango has some support for this with its 
Reader/Writer model in tango.io.protocol, but even that is somewhat 
limited to specific categories of protocols.  For example, I recently 
tried using it for ASN.1 transcoding and it just isn't sufficient.


Sean


More information about the Digitalmars-d-learn mailing list