Why does toString() exist? It seems useless.

Lars Ivar Igesund larsivar at igesund.net
Mon Aug 18 04:06:15 PDT 2008


Don wrote:

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

It is not useless, but it would indeed be useful to be able to pass a format
string to it - toString(char[] fmt) - or similar.

Note that if structs could have interfaces, then this wouldn't really be a
problem.

-- 
Lars Ivar Igesund
blog at http://larsivi.net
DSource, #d.tango & #D: larsivi
Dancing the Tango


More information about the Digitalmars-d-learn mailing list