Why can't we define re-assignable const reference variable?

Derek Parnell derek at psych.ward
Tue Feb 19 13:19:48 PST 2008


On Tue, 19 Feb 2008 12:49:09 -0800, Walter Bright wrote:

> invariant(C)[] c;
> invariant(int)[] i;
> 
> should both have immutable array contents, i.e. if the contents of a 
> reference is invariant then the reference itself should be invariant.
> 
> The concept of having a mutable reference to immutable data is 
> fundamentally flawed because it breaks the concept of what a reference 
> is. A reference is not separable from what it refers to. If they were 
> separable, they would be syntactically and semantically the same thing 
> as pointers, and there would be no point whatsoever to even having a 
> reference type.

Ok given this position and that 'string' is 'invariant(char)[]' then

   string s;  

makes 's' a reference to an immutable array, and thus 's' should also be
immutable, right? If so, does that mean that I cannot bind 's' to another
literal - that is change the bits in 's' such that the .ptr and maybe
.length properties have different values?

  string s = "abc'; // Initialized ok.
  s = "def";  // Should this fail because 
              // I'm now changing an immutable reference?

If your position re immutable references to hold, and if our codes needs
mutable 'references' to immutable data, and thus we need to use pointers,
what is the 'pointer' syntax for dynamic arrays that preserves array bounds
checking?

-- 
Derek Parnell
Melbourne, Australia
skype: derek.j.parnell



More information about the Digitalmars-d mailing list