Thread overview
games with toString()
Dec 11, 2005
Kris
Dec 11, 2005
JT
Dec 11, 2005
Sean Kelly
Dec 11, 2005
Kris
December 11, 2005
toString(int) returns a shared, global slice for small numbers:

import std.string;
import std.stdio;

void main()
{
        char[] c = toString(1);
        writefln (c);
        c[0] = '!';
        writefln (toString(1));
}

emits:

1
!

Read-only arrays would resolve this, and a whole lot more besides  :-)


December 11, 2005
In article <dng17i$6n6$1@digitaldaemon.com>, Kris says...
>
>toString(int) returns a shared, global slice for small numbers:
>
>import std.string;
>import std.stdio;
>
>void main()
>{
>        char[] c = toString(1);
>        writefln (c);
>        c[0] = '!';
>        writefln (toString(1));
>}
>
>emits:
>
>1
>!
>
>Read-only arrays would resolve this, and a whole lot more besides  :-)
>
>

WOOOOOOW! ok yeah Ive had weird problems like this and Im learning more every day about how D handles these things. Thanks very much for pointing this out - ive got .dups sprinkled all over my code becuase Im still trying to figure out what these problems are - but you just gave me some important information. Its quite straightforward but D is certainly a new paradigm one has to get used to.


December 11, 2005
Kris wrote:
> toString(int) returns a shared, global slice for small numbers:
> 
> import std.string;
> import std.stdio;
> 
> void main()
> {
>         char[] c = toString(1);
>         writefln (c);
>         c[0] = '!';
>         writefln (toString(1));
> }
> 
> emits:
> 
> 1
> !
> 
> Read-only arrays would resolve this, and a whole lot more besides  :-)

I suspect this is a const string and attempting the above on Linux would result in a run-time error.  But it's a good point nevertheless :-)


Sean
December 11, 2005
"Sean Kelly" <sean@f4.ca> wrote
> Kris wrote:
>> Read-only arrays would resolve this, and a whole lot more besides  :-)
>
> I suspect this is a const string and attempting the above on Linux would result in a run-time error.  But it's a good point nevertheless :-)

You just gave me an idea ;-)