[Issue 3814] Mutation of immutable string

2015-06-09 Thread via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=3814

Andrei Alexandrescu and...@erdani.com changed:

   What|Removed |Added

Version|2.040   |D2

--


[Issue 3814] Mutation of immutable string

2010-02-18 Thread d-bugmail
http://d.puremagic.com/issues/show_bug.cgi?id=3814


Steven Schveighoffer schvei...@yahoo.com changed:

   What|Removed |Added

 Status|NEW |RESOLVED
 Resolution||INVALID


--- Comment #1 from Steven Schveighoffer schvei...@yahoo.com 2010-02-18 
10:33:46 PST ---
s is not immutable, only the data it points to.

s is of type immutable(char)[], which means the array itself can be added to or
reassigned to another slice of data, but the data it points to is immutable. 
It is read an array of immutable chars instead of an immutable array of
chars.

If you want an immutable string, do:

immutable string s = ab;

Which you will not be able to change the length of.

Also could do:

immutable char[] s = ab;

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
--- You are receiving this mail because: ---


[Issue 3814] Mutation of immutable string

2010-02-18 Thread d-bugmail
http://d.puremagic.com/issues/show_bug.cgi?id=3814


Steven Schveighoffer schvei...@yahoo.com changed:

   What|Removed |Added

 CC||schvei...@yahoo.com


--- Comment #3 from Steven Schveighoffer schvei...@yahoo.com 2010-02-18 
11:23:09 PST ---
It does have immutable traits.  With D's immutable strings, the *shared* part
is immutable.  That is, you can share strings because of the hybrid
value/reference type that is an array (shared data, local length).  It's a very
different concept to other programming languages.

Because strings are not true reference types, changing the length of one string
does not affect another string, even if both point to the same data.

Think about how Java has immutable strings, but you can reassign a string
variable.  That does not make the strings themselves not immutable!  However,
immutable strings in Java are pure reference types (even the length is shared),
so in order to take a slice of a string, you must make an entirely new
object, but the same data (except for the length) is referenced.  The same is
not true in D where the string length is not shared, and therefore does not
need to be immutable.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
--- You are receiving this mail because: ---