toString ugliness

Steve Schveighoffer schveiguy at yahoo.com
Sat Dec 6 19:55:35 PST 2008


On Sat, 06 Dec 2008 11:21:11 -0500, Jerry wrote:

> toString() doesn't work inside a class member function.
> 
> import std.string;
> class A {
>   void f() {
>     string s = toString(5);
>   }
> }
> 
> This errors with junk.d(19): function object.Object.toString () does not
> match parameter types (int)
> 
> This is a nuisance and a wart (though not a bug per-se).
> 
> If the language really can't handle distinguishing this.toString() from
> toString(int), then std.string.toString really should have a different
> name.
> 
> It can be solved somewhat by documenting clearly that to!(string)(int)
> be used instead, which seems silly to me.  I'm irritated by the 3 extra
> chars required to type a to!(type) template.
> 
> .toString() works around the problem, but why should it be needed?  This
> is unfortunate.
> 
> Does this bother anyone else?  If not, I'll return to my lurking cave
> :-)

Doesn't bother me at all.  Using tango, I usually rename the appropriate 
toString module imports.  i.e.:

import Int = tango.text.convert.Integer;

Int.toString(5);

But you'd probably hate that.  That's *4* more characters!

Or you could alias it at the top of the module you want to use it in.

alias toString toStr;

The object.toString function is so well known, and so intuitive, I doubt 
there would be any possible way it would be changed.  Nor would I want 
that to change.

-Steve



More information about the Digitalmars-d mailing list