Thread overview | ||||||
---|---|---|---|---|---|---|
|
December 11, 2005 games with toString() | ||||
---|---|---|---|---|
| ||||
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 Re: games with toString() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kris | 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 Re: games with toString() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kris | 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 Re: games with toString() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | "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 ;-)
|
Copyright © 1999-2021 by the D Language Foundation