string types: const(char)[] and cstring

Derek Parnell derek at psych.ward
Sat May 26 23:35:16 PDT 2007


On Sat, 26 May 2007 22:27:18 -0700, Walter Bright wrote:

> 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.

While that is interesting, it has not much to do with what I was saying.

You said "strings should be immutable" and I saying that seems odd because
my experience is that most strings are meant to be changed. 

So now I'm thinking that we are talking about different things when we use
the word "string". I'm guessing you are really referring to compile-time
generated string data (e.g. literals) rather than run-time generated string
data.


>> 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. 

Huh??? Isn't that what I just said? Now I'm even more confused about these
terms. They are just not intuitive, are they?

> Const is only immutable through the 
> reference - another reference to the same data can change it.

Ok ... so this below won't fail ...

  void func(const char[] parm)
  {
      char [] q;
      q = parm;
      q[0] = 'a';
  }

or is the "q = parm" not really permitted.

>> 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;


Given the syntax on the form "  void func(<X> char[] parm) ", is the table
below true ...

*-------------------------------------*
| <X>         + parm.ptr  |  parm[0]  |    
|-------------+-----------------------+
| const       | mutable   | immutable |
| final       | immutable | mutable   |
| invariant   | immutable | immutable |
|             | mutable   | mutable   |
*-------------------------------------*


I'm sorry I'm a bit slow on this ... but what is the difference between
"invariant" and "const final" ? Is it that "invariant" is sort of a global
effect but "const final" is only in effect for the specific reference it
occurs on.

I'm not looking forward to reading the docs on this. I hope you get a lot
of people to edit the docs to make it understandable for everyone.

-- 
Derek Parnell
Melbourne, Australia
"Justice for David Hicks!"
skype: derek.j.parnell



More information about the Digitalmars-d-announce mailing list