Why does toString() exist? It seems useless.
Robert Fraser
fraserofthenight at gmail.com
Tue Aug 19 12:04:43 PDT 2008
Benji Smith wrote:
> Don wrote:
>> I cannot understand the rationale for a toString() member function
>> which doesn't support formatting.
>
> I agree.
>
> In Java, the toString() method *must* exist on the base Object class,
> because of constructs like this:
>
> String s = "hello" + (new World());
>
> Without an implementation of toString(), it'd be impossible to support
> those kinds of automatic String conversion and concatenation (which
> actually do end up being pretty handy in logging & debugging statements).
>
> But since D doesn't support implicit String conversion, it seems pretty
> pointless.
I think both Tango and phobos allow objects to be converted to strings
when specified as parameters to a formatting function -- i.e.
Stdout.format("{0}", obj) will call obj.toString().
More information about the Digitalmars-d-learn
mailing list