Extended Type Design: further examples

Derek Parnell derek at nomail.afraid.org
Mon Mar 19 18:20:07 PDT 2007


On Mon, 19 Mar 2007 17:50:28 -0700, Andrei Alexandrescu (See Website For
Email) wrote:

> Derek Parnell wrote:
>> Hmmm ... so the difference between 'const' and 'invariant' is that const is
>> only effective for one level deep, but 'invariant' is effective for all
>> levels, right?
> 
> Nonono. Const means: it might have originated as a variable, and 
> somebody, somewhere, might hold a mutating reference to this and call it 
> "my precious".
> 
> Invariant is: nobody ever holds a mutating reference to it.

Yeah ... but that's not what I was talking about. But be that as it may, it
seems you are saying that the *only* difference between 'const' and
'invariant' is that 'invariant' means that every write attempt (direct and
indirect) is denied, but 'const' only prevents direct write attempts.

By 'direct' I mean explicitly using the symbol in the statement, and not
another symbol that happens to contain the same address of the data.


But I was talking earlier about the situation where something contains
reference types or structs, and what effect 'const' and 'invariant' have on
the things indirectly accessible via member data.

 struct Foo
 {
    char[] s;
 }

 void Func(const Foo fc, invariant Foo fi)
 {
    fc.s = "abc"; // fails ???
    fc.s[0] = 'a'; // okay ???

    fi.s = "abc"; // fails ???
    fi.s[0] = 'a'; // fails ???
 }

 Foo a;
 Foo b;
 Func(a,b); // fails ???

 const Foo ac;
 const Foo bc;
 Func(ac,bc); // fails ???

 invariant Foo ai;
 invariant Foo bi;
 Func(ai,bi); // fails ???

 Func(ac,bi); // okay ???


-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Justice for David Hicks!"
20/03/2007 12:04:01 PM



More information about the Digitalmars-d mailing list