Why does toString() exist? It seems useless.

BCS ao at pathlink.com
Mon Aug 18 10:34:13 PDT 2008


Reply to don,

> I cannot understand the rationale for a toString() member function
> which doesn't support formatting.
> 
> I don't think I've ever made a class which only has a single format
> option. If the class contains any integers, I want to specify whether
> it should use hex or decimal, leading zeros, sign. If it contains
> floating point numbers, it's number of digits, scientific notation,
> hex or decimal, and what to do with NaNs. And of course there are
> locales.
> 
> Seriously, I cannot remember ever not having this requirement, so I
> cannot imagine a use for toString().
> 
> C++ got around this by giving state to the iostream classes. As far as
> I can tell, neither Phobos nor Tango provide any support at all.
> 
> Practical example: BigInt (which I'm developing in both Tango and
> Phobos). It is a struct. How can it support I/O? I can't work out how
> to do it in either library.
> 

I think the point is to have a least common denominator "Just give me *something*" 
method for debugging and the like. Anything more demanding is to complicated 
for the base class as it will never be flexible enough.




More information about the Digitalmars-d-learn mailing list