Limited Semi-PolyMorphic (LSP) structs?

Era Scarecrow rtcvb32 at yahoo.com
Tue Aug 27 18:46:59 PDT 2013


  It just came to mind that what I want is almost more a unioned 
struct. Perhaps this will come up with something closer to what I 
am really looking for.

assuming we could use a union, it would be closer to:

//type is the identifier of which one it's going to be using
union AB {
   iA ia;
   iB ib;

   void A(){}
   void B(){}
   void C(){}
}

struct iA {
   char type;
   void A();
   void C();
}
struct iB {
   char type;
   void A();
   void B();
}

  Now last i checked putting functions calls in unions wasn't 
really a good idea or supported, however we have a problem where 
iA and iB don't know about the union AB, and they will still try 
to call the local struct's data first (or the namespace) before 
anything else. This means if C() get's called, and the type is 
suppose to use iB, then when C() calls A(), it gets iA's A() and 
not iB's A().

  If however we try to do inner structs, then:
struct AB {
   char type;
   iA ia;
   iB ib;

   struct iA {
     void real_A() { //regardless what we call, the outer struct 
gets the calls
       A(); //line 9
       B();
       C();
     }
     void real_C() {
       A();
       B();
       C();
     }
   }

   struct iB {
     void real_A() {
       A();
       B();
       C();
     }
     void real_B() {
       A();
       B();
       C();
     }
   }

   void A() { //calls real_A or real_B
         switch (type) {
             case 'A': ia.real_A();
             case 'B': ib.real_A();
             default: throw new Exception("");
         }
   }
   void B() {
         switch (type) {
             case 'B': ib.real_B();
             default: throw new Exception("");
         }
   }
   void C() {
     ia.real_C();
   }
}

   So far this does seem to want to work, but last I checked I 
thought this was illegal, the reason being the inner structs 
needed a pointer to their outer parent which wasn't a class, at 
which point there was something just difficult about it.

test.d(9): Error: this for A needs to be type AB not type iA
test.d(10): Error: this for B needs to be type AB not type iA
test.d(11): Error: this for C needs to be type AB not type iA
test.d(14): Error: this for A needs to be type AB not type iA
test.d(15): Error: this for B needs to be type AB not type iA
test.d(16): Error: this for C needs to be type AB not type iA
test.d(22): Error: this for A needs to be type AB not type iB
test.d(23): Error: this for B needs to be type AB not type iB
test.d(24): Error: this for C needs to be type AB not type iB
test.d(27): Error: this for A needs to be type AB not type iB
test.d(28): Error: this for B needs to be type AB not type iB
test.d(29): Error: this for C needs to be type AB not type iB

  I recall there being something to help get around this, but a 
delegate didn't seem quite like the right answer...

  I really really don't want to rely on classes, passing a 
reference to the caller may work but adds unnecessary stuff (plus 
you have to explicitly call the referenced rather than having it 
feel like what it should). 'alias this' might help (as with my 
template attempt), but it quickly becomes a chore to keep up 
things and gets ugly fast.

  Mmm..


More information about the Digitalmars-d-learn mailing list