Why does toString() exist? It seems useless.
JAnderson
ask at me.com
Wed Aug 20 08:24:13 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.
I think you can do much the same as iostream and still take advantage of
toString. Something like:
string foo = a.toString() ~ format(a, "...");
Where format would be special overloaded template (with specialization)
that does conversions.
-Joel
More information about the Digitalmars-d-learn
mailing list