Extended Type Design: further examples

Derek Parnell derek at nomail.afraid.org
Mon Mar 19 16:01:37 PDT 2007


On Mon, 19 Mar 2007 15:05:29 -0700, Andrei Alexandrescu (See Website For
Email) wrote:


I wish it wasn't so hard to get a simple straight answer from the experts.

So far I think /The W&A Show/ is saying that 'const' and 'invariant' only
apply to reference data types (objects, variable-length arrays, and
pointers) and structs. They do not apply *in any way, shape or form* to any
other data type.

  const char[] s;    // ok
  const char   c;    // meaningless, the 'const' is ignored.
  const classFoo f;  // okay
  const double* b;   // okay;
  const double  d;   // meaningless, the 'const' is ignored.
  const structBar q; // okay

'const' means you cannot change the stuff the reference is referencing or
the members of the struct, *but* for reference types you can change the
reference value.

  s[0] = 'a'; // fails
  s    = "new data"; //ok
  s.length = 2; // ok

  c    = 'a'; // ok

  f.bar = 'a'; // fails
  f = new classFoo(); // okay

  *b    = 1.0; // fails
  b     = &someDouble; // ok

  d     = 1.0; // okay

  q.foo = 'a'; // fails

'invariant' means you cannot change the stuff the reference is referencing
or the members of the struct, *and* for reference types you cannot change
the reference value.

 (If you one used 'invariant' rather than 'const' in above declarations...)
  s[0] = 'a'; // fails
  s    = "new data"; // fails
  s.length = 2; // fails

  c    = 'a'; // ok

  f.bar = 'a'; // fails
  f = new classFoo(); // fails

  *b    = 1.0; // fails
  b     = &someDouble; // fails

  d     = 1.0; // okay

  q.foo = 'a'; // fails


However, if this is the interpretation of 'invariant', how does one ever
get to set the 'invariant' thing's value?

   invariant byte[] vCodes;
   . . . 
   vCodes = LoadCodesFromFile();  // fails, no???


> By the way, a couple of days ago I've had an idea. Many people here ask
> for a "string" alias because char[] is a tad hard to type and uneasy on
> the eyes. Probably a good candidate would be:
> 
> alias invariant char[] string;
>
> The resulting type is pretty damn slick:
> 
> 1. You can alias it and slice it til you get blue in the face - no
> confusion ever, because nobody can overwrite the contents underneath
> your alias.

 void func( string s )
 {

     string t;
     char[] u;
     u = s;
     u[0] = 'a'; // fails.
     u = s[1..4];
     u[0] = 'a'; // fails
     u = s.dup;
     u[0] = 'a' // ok

     t = s; // fails???

 }
  

> 3. The ~= operator works (somewhat surprisingly).
 void func( string s )
 {
     string t;
     char[] u;

     u ~= s; // ok
     s ~= 'a'; // fails ???

     t ~= s; // fails???
 }

    
> 4. It's very, very rare that you want to modify some random character in
> a string, and when you do, use a char[] and then copy it back into a
> string, or rebuild the string from slices!

It is not so rare in the type of applications that I do. 

Are you saying that I can do this...?

 string func( string s )
 {
     char[] u;
     u = s.dup;
     u[somerandomspot] = 'a';
     return u;
 }
  
or this ... ?

 void func( string ref s )
 {
     char[] u;
     u = s.dup;
     u[somerandomspot] = 'a';
     s = u;
 }

or this ... ?

 string func( string ref s )
 {
     char[] u;
     string t;
     u = s.dup;
     u[somerandomspot] = 'a';
     t = u;
 }

What I don't get here is the phrase "copy it back into a string". This
sounds ambiguous to me. It could mean, "construct a new string containing
the updated data", or "replace the original string reference with a
reference to the updated data". But I was starting to think that
'invariant' meant that you couldn't change the reference value at all, so I
don't see how one could change an 'invariant' parameter or construct an
'invariant' local /variable/.

 
> So it looks like we have solved the string issue in an unexpectedly
> elegant way - by simply combining two features: invariant and array.

I hope so.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Justice for David Hicks!"
20/03/2007 9:28:14 AM



More information about the Digitalmars-d mailing list