string types: const(char)[] and cstring

Walter Bright newshound1 at digitalmars.com
Sat May 26 22:27:18 PDT 2007


Derek Parnell wrote:
> We seem to have different experience. Most of the code I write deals with
> changing strings - in other words, manipulating strings is very very common
> in the sorts of programs I write.

You'll still be able to concatenate and slice invariant strings. You can 
also cast a char[] to an invariant, when you're done building it.

> So 'const(char)[] x' means that I can change x.ptr and x.length but I
> cannot change anything that x.ptr points to, right?

Right.

> And  'invariant(char)[] x' means that I cannot change x.ptr or x.length and
> I cannot change anything that x.ptr points to, right?

Wrong. The difference between const and invariant is that invariant is 
truly, absolutely, immutable. Const is only immutable through the 
reference - another reference to the same data can change it.

> So what syntax is to be used so that x.ptr and x.length cannot be changed
> but the characters referred to by 'x' can be changed?

final char[] x;



More information about the Digitalmars-d-announce mailing list