Logical Const using a Mutable template

Jesse Phillips jessekphillips+D at gmail.com
Tue Nov 30 10:21:18 PST 2010


Jesse Phillips Wrote:

> The rules for this are:
> 
> * Only mutable data can be assigned to a Mutable
> * Modification of referenced fields can be modified (inner class fields, pointer targets)
> 
> I'll be back to finish this in a bit.

* Value types can be modified if the encapsulating class
      is not declared const/immutable

And I have come up with 2 major concerns related to immutable classes. Would an inner class be placed in read-only memory  in instantiating an immutable class:

class A {
    Mutable!(Inner) innerclass;
    this() { innerclass = new Inner; } // Is innerclass placed in read-only memory
}

new immutable(A);

And what about when an immutable instance is passed to a thread or network. Would the innerclass be placed in read-only memory for the new thread/machine.

And looking over it again, I found that I wasn't using the opAssign const functions, they didn't meet the requirements. So I think a working alias this would allow the template to be: (The important check being that Mutables are unqualified)

struct Mutable( T ) if ( is( T : Unqual!T ) ) {
     private T _payload;

     this( T t ) {
         _payload = t;
     }

     @trusted @property ref T get( )( ) const {
         T* p = cast( T* )&_payload;
         return *p;
     }
     alias get this;
}

https://gist.github.com/721066


More information about the Digitalmars-d mailing list