www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3814] New: Mutation of immutable string

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3814

           Summary: Mutation of immutable string
           Product: D
           Version: 2.040
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: druntime
        AssignedTo: sean invisibleduck.org
        ReportedBy: bearophile_hugs eml.cc



import std.stdio: writeln;
void main() {
    string s = "ab";
    s.length += 1;
    //writeln(typeid(typeof(s))); // Prints:
    s[2] = 'a'; // test5.d(6): Error: s[2] isn't mutable
}

Why is length+=1 allowed if s is immutable?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 18 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3814


Steven Schveighoffer <schveiguy yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID



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: -------
Feb 18 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3814




A string can't be defined immutable (and can't enjoy the advantages of
immutability, like being a safer hash key) if it can change size. This is a
half-immutability of strings. Maybe there is no good solution to this...

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 18 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3814


Steven Schveighoffer <schveiguy yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy yahoo.com



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: -------
Feb 18 2010