toString ugliness

bearophile bearophileHUGS at lycos.com
Sun Dec 7 04:59:31 PST 2008


Ary Borenszweig:
> It bothers me. Maybe it doesn't bother anyone else in this newsgroup 
> because they already fell in that trap and they know the solution or the 
> workarounds.

I too have fallen in this little trap, but after the first time you learn to add a "." before toString. I use str()/repr() functions from my dlibs to avoid name clashes with toString() (see below), so I think that it may be better for the method and the global function to have different names.

Python avoids such problem because the function and the method have different names: the global built-in functions are named str() and repr() and the methods are __str__() and __repr__(). (each function calls the relative method. If __str__ is absent, __repr__ is called as fallback). str() gives a human-readable textual representation of something, and repr() gives a string representation that whenever possible is the text you have to write to define that thing in the code, so generally eval(repr(x)) == x.

Bye,
bearophile



More information about the Digitalmars-d mailing list