Constant relationships between non-constant objects

Chris Nicholson-Sauls via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 19 01:50:54 PDT 2014


Ok, I must be missing something...

Just to make sure I'm understanding the OP: he wants to create 
two classes, A and B, such that each instance of A must be given 
a B to cling to, and which cannot be reassigned?  In what way is 
the following insufficient?


class B {/*...*/}

class A
{
     this(B someB)
     {
         _b = someB;
     }

     invariant
     {
         assert(someB !is null);
     }

     private B _b;

     @property B b() const
     {
         return _b;
     }
}


Or is it that he wants to prevent even the code in method bodies 
or same-module functions from reassigning it?  In that case, I 
see two basic things to do:

  1) Isolate A (and B if it makes sense) into a module all by its 
lonesome
  2) Don't write `_b = ...`

#1 is optional.

Am I missing something, or is it really that simple?  (Not that a 
guaranteed non-rebindable type wouldn't be a nice thing to have, 
mind you.)


More information about the Digitalmars-d mailing list