Why does toString() exist? It seems useless.

Fawzi Mohamed fmohamed at mac.com
Sat Aug 23 13:56:12 PDT 2008


On 2008-08-23 00:28:19 +0200, Sean Kelly <sean at invisibleduck.org> said:

> 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.

Yes I think that it is the correct approach to have special streams for 
serialization, as long as they can then be hooked to the "normal" 
streams then everything is ok.

It is a pity that Reader/Writer are not sufficient, could it be 
extended to support it, or a different approach is needed?
Actually my needs are smaller, I would love to have at least a textual 
streaming protocol (xml based?) that works with Reader/Writer for quick 
dump/read of my objects..

Fawzi



More information about the Digitalmars-d-learn mailing list