Semantics of toString

Michal Minich michal at minich.sk
Thu Nov 5 05:28:58 PST 2009


Hello Justin,

> I assert that the semantics of "toString" or similarly named/purposed
> methods/functions in many PL's (including and not limited to D) is
> ill-defined.
> 
> To put this statement into perspective, I would be most appreciative
> of D NG readers responding with their own idea(s) of what the
> semantics of "toString" are (or should be) in a language agnostic
> ideology.
> 
> If there are more than, say, two or three different views on the said
> semantics then my "ill-definition" assertion is surely correct.
> 
> If there are no replies on this matter, then guess I'm left
> concludeless.
> 
> Just thinking in the language round-up that this is (just another) one
> of the things we should address as a community.
> 
> So what does "toString" mean to you?
> 
> **beers,
> Justin
> **caveat: free beer offer available in-store only
> 

My practice tells me to use toString only for debugging - to quickly get 
string representation of object in human readable format - nothing else ever. 
So it is good that toString is part of D object class.

It quite unsuitable e.g. for serializing object to xml/html or other formats. 
You may find yourself later finding out that your object should not only 
be toString-ed to xml, but now to json... Better to use specific method for 
specific purpose.

what matters me more of object methods, is opEquals being part of them. But 
that is different story.





More information about the Digitalmars-d mailing list