www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - immutable/mutable strings

reply Zane <zane.sims gmail.com> writes:
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
Nov 19 2008
parent reply "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Wed, Nov 19, 2008 at 8:13 PM, Zane <zane.sims gmail.com> wrote:
 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.
That's all correct.
 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.
Yes. The whole section on const and invariant is D2 only. If you click the "D 1" link at the top left, you'll get to the D 1 specification. The Digital Mars website kind of .. "guides" you into D2 with little mention of D1.
Nov 19 2008
parent reply Zane <zane.sims gmail.com> writes:
Jarrett Billingsley Wrote:

 On Wed, Nov 19, 2008 at 8:13 PM, Zane <zane.sims gmail.com> wrote:
 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.
That's all correct.
 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.
Yes. The whole section on const and invariant is D2 only. If you click the "D 1" link at the top left, you'll get to the D 1 specification. The Digital Mars website kind of .. "guides" you into D2 with little mention of D1.
Thanks for your quick response. So then as in the example given, in D2, is this initialization allowed or was it a typo/mistake in the example?: char[] s1 = "hello world"; Thanks again, Zane
Nov 19 2008
next sibling parent "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Wed, Nov 19, 2008 at 8:35 PM, Zane <zane.sims gmail.com> wrote:
 Jarrett Billingsley Wrote:

 On Wed, Nov 19, 2008 at 8:13 PM, Zane <zane.sims gmail.com> wrote:
 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.
That's all correct.
 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.
Yes. The whole section on const and invariant is D2 only. If you click the "D 1" link at the top left, you'll get to the D 1 specification. The Digital Mars website kind of .. "guides" you into D2 with little mention of D1.
Thanks for your quick response. So then as in the example given, in D2, is this initialization allowed or was it a typo/mistake in the example?: char[] s1 = "hello world";
I'm not sure, I haven't touched D2 yet.
Nov 19 2008
prev sibling parent Brad Roberts <braddr puremagic.com> writes:
Zane wrote:
 Jarrett Billingsley Wrote:
 
 On Wed, Nov 19, 2008 at 8:13 PM, Zane <zane.sims gmail.com> wrote:
 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.
That's all correct.
 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.
Yes. The whole section on const and invariant is D2 only. If you click the "D 1" link at the top left, you'll get to the D 1 specification. The Digital Mars website kind of .. "guides" you into D2 with little mention of D1.
Thanks for your quick response. So then as in the example given, in D2, is this initialization allowed or was it a typo/mistake in the example?: char[] s1 = "hello world"; Thanks again, Zane
Please file a bug. The whole cppstrings.html page hasn't been updated to properly reflect 2.0 const semantics. Later, Brad
Nov 19 2008