Logical const

Simen kjaeraas simen.kjaras at gmail.com
Mon Nov 29 14:18:46 PST 2010


Walter Bright <newshound2 at digitalmars.com> wrote:

>> immutable - Data will never, ever change, through any reference.
>> const - Data will never change through this reference.
>> newlevel - Data will never logically change through this reference.
>
> The problem with newlevel is (to repeat myself) it is unverifiable.

As long as it is transitive, I believe it is verifiable:

struct S {
     int n;
}

struct foo {
     int n;
     foo* p;
     newlevel S s;
     newlevel int m;
     void bar( ) newlevel {
         m++; // works, m is newlevel
         n++; // compile-time error, n is const for newlevel this
         s.n = 3; // Works, s.n is newlevel
         p.n = 4; // compile-time error, p is newlevel, p.n is const for
                  //  newlevel this
         p.m++; // Works, p.m is newlevel
     }
     void baz( ) const {
         m++; // compile-time error, m is const for const this
     }
}

void qux( newlevel foo f ) {}
void quux( ref foo f ) { f.n++; }

void main() {
     const foo f;
     f.bar( ); // compile-time error, f is const, cannot call newlevel
               //  functions on it
     qux( f ); // compile-time error, const(foo) cannot be implicitly
               //  cast to newlevel(foo)

     newlevel foo g;
     quux( g ); // compile-time error, newlevel(foo) cannot be implicitly
                //  cast to foo
}

This leaves const as a strong, transitive guarantee, and newlevel as a
transitive guarantee that only newlevel members may be changed.


I am not convinced that such an extension of the type system should be
made, but I believe you are wrong in that it is not verifiable. but
please, do show me my mistake.

-- 
Simen


More information about the Digitalmars-d mailing list