logical const without casts!

Simen Kjaeraas simen.kjaras at gmail.com
Thu Sep 29 10:06:55 PDT 2011


On Thu, 29 Sep 2011 16:54:24 +0200, Steven Schveighoffer  
<schveiguy at yahoo.com> wrote:

> I just thought of an interesting way to make a logical const object  
> without casts.  It requires a little extra storage, but works without  
> changes to the current compiler (and requires no casts).
[snip]
> What do people think about this?

This is what I think about it:

class A {
     int n;
     void delegate( ) dg;
}

pure
A createAnA( int n ) {
     A result = new A;
     result.n = n;
     result.dg = (){ result.n++; };
     return result;
}

void main( ) {
     immutable A tmp = createAnA( 3 );
     assert( tmp.n == 3 );
     tmp.dg();
     assert( tmp.n == 3 );
}

-- 
   Simen


More information about the Digitalmars-d mailing list