View mode: basic / threaded / horizontal-split · Log in · Help
June 21, 2005
std.string.toString() methods cause confusion
Hi,

Is it just me or do the std.string.toString() methods in phobos seem to be
awkwardly named? Am I pointing out the obvious and/or going about this the wrong
way or will I always have to use the fully qualified version (or write my own :)
?

eg.

private import std.string;

class A {
public void doSomething() {
char[] a  = "abcd" ~ toString(10); // bad - calls Object.toString() and fails
char[] b  = "abcd" ~ std.string.toString(10); // ok
}
}

int main() {
A a = new A();
a.doSomething();
}
June 21, 2005
Re: std.string.toString() methods cause confusion
"pmoore" <pmoore_member@pathlink.com> wrote in message 
news:d9a7i8$112m$1@digitaldaemon.com...
> Hi,
>
> Is it just me or do the std.string.toString() methods in phobos seem to be
> awkwardly named? Am I pointing out the obvious and/or going about this the 
> wrong
> way or will I always have to use the fully qualified version (or write my 
> own :)
> ?

I used to do this too.. until someone introduced me to the global scope 
operator.

char[] b  = "abcd" ~ .toString(10); // calls std.string.toString()

Notice the . in front of toString().
June 22, 2005
Re: std.string.toString() methods cause confusion
On Tue, 21 Jun 2005 19:59:51 -0400, Jarrett Billingsley  
<kb3ctd2@yahoo.com> wrote:
> "pmoore" <pmoore_member@pathlink.com> wrote in message
> news:d9a7i8$112m$1@digitaldaemon.com...
>> Hi,
>>
>> Is it just me or do the std.string.toString() methods in phobos seem to  
>> be
>> awkwardly named? Am I pointing out the obvious and/or going about this  
>> the
>> wrong
>> way or will I always have to use the fully qualified version (or write  
>> my
>> own :)
>> ?
>
> I used to do this too.. until someone introduced me to the global scope
> operator.
>
> char[] b  = "abcd" ~ .toString(10); // calls std.string.toString()
>
> Notice the . in front of toString().

I'd have expected an alias to work also, eg.

private import std.string;
alias std.string.toString toString;

but it doesn't. It errors in the same way.

I want to know why? I don't see why toString on it's own calls  
Object.toString.

Regan
June 22, 2005
Re: std.string.toString() methods cause confusion
"Regan Heath" <regan@netwin.co.nz> wrote in message 
news:opssqwqybk23k2f5@nrage.netwin.co.nz...
> I'd have expected an alias to work also, eg.
>
> private import std.string;
> alias std.string.toString toString;
>
> but it doesn't. It errors in the same way.
>
> I want to know why? I don't see why toString on it's own calls 
> Object.toString.

You have to put the alias in the class.  Otherwise, it's at module level, 
and the class's toString is checked before going to the module-level alias. 
So:

class A
{
   alias std.string.toString toString;
   void fork()
   {
       int x=5;
       char[] s=toString(x);
   }
}

Works.
June 22, 2005
Re: std.string.toString() methods cause confusion
On Tue, 21 Jun 2005 21:22:38 -0400, Jarrett Billingsley  
<kb3ctd2@yahoo.com> wrote:
> "Regan Heath" <regan@netwin.co.nz> wrote in message
> news:opssqwqybk23k2f5@nrage.netwin.co.nz...
>> I'd have expected an alias to work also, eg.
>>
>> private import std.string;
>> alias std.string.toString toString;
>>
>> but it doesn't. It errors in the same way.
>>
>> I want to know why? I don't see why toString on it's own calls
>> Object.toString.
>
> You have to put the alias in the class.  Otherwise, it's at module level,
> and the class's toString is checked before going to the module-level  
> alias.
> So:
>
> class A
> {
>     alias std.string.toString toString;
>     void fork()
>     {
>         int x=5;
>         char[] s=toString(x);
>     }
> }
>
> Works.

LOL.. I completely missed the fact that a class was involoved. I just  
copy/pasted the code, ran it, added the alias and complained.

Regan
June 22, 2005
Re: std.string.toString() methods cause confusion
Thanks for the replies. 

I actually posted this at half past midnight last night in a sleepy daze and
then realised I could have used the global operator as Jarrett suggested. The
alias is an interesting alternative though. Thanks to both of you.

In article <d9ae6a$18hm$1@digitaldaemon.com>, Jarrett Billingsley says...
>
>"Regan Heath" <regan@netwin.co.nz> wrote in message 
>news:opssqwqybk23k2f5@nrage.netwin.co.nz...
>> I'd have expected an alias to work also, eg.
>>
>> private import std.string;
>> alias std.string.toString toString;
>>
>> but it doesn't. It errors in the same way.
>>
>> I want to know why? I don't see why toString on it's own calls 
>> Object.toString.
>
>You have to put the alias in the class.  Otherwise, it's at module level, 
>and the class's toString is checked before going to the module-level alias. 
>So:
>
>class A
>{
>    alias std.string.toString toString;
>    void fork()
>    {
>        int x=5;
>        char[] s=toString(x);
>    }
>}
>
>Works. 
>
>
Top | Discussion index | About this forum | D home