immutable/mutable strings

Zane zane.sims at gmail.com
Wed Nov 19 17:13:45 PST 2008


Hey all,

I am trying to get a full understanding of mutable/immutable string syntax.  I understand it to be true that 'char[]' is mutable and that 'string' is immutable and is the same as invariant(char)[].  If I am incorrect on any of this, please let me know.

The page: http://www.digitalmars.com/d/2.0/arrays.html gives the following example as an initialization error:

char[] str1 = "abc";                // error, "abc" is not mutable

I assume this is an error because of incompatible types (trying to set a mutable type to an immutable value.

However, and this confuses me.  The page: http://www.digitalmars.com/d/2.0/cppstrings.html

char[] s1 = "hello world";
char[] s2 = "goodbye      ".dup;
s2[8..13] = s1[6..11];		// s2 is "goodbye world"

If what I have assumed is true so far, why is the following line from above allowed?:

char[] s1 = "hello world";

On a slightly different note, why does the compiler allow me to set an immutable value to a mutable type if it is an error?  I am using dmd v1.033 -- Is this for v2.xx only?  Thanks for any information that you can provide and sorry if these questions have already been answered.

Thanks,
Zane




More information about the Digitalmars-d mailing list