Struct polymorphism?

Era Scarecrow rtcvb32 at yahoo.com
Sun Oct 7 02:52:58 PDT 2012


  What are the possibilities of struct polymorphism? What would be 
the issues with it? What if we wanted to use it in a limited 
sense?

  Currently I'm experimenting with it since classes are too bulky 
for what I need, yet I really need some type of behavior/dynamic 
polymorphism. So far I have a working model. It takes the 
following limitations:

   1) struct size can't size (no loss of data).
   2) every structure aside from the original needs a copy of the 
original (and only that)
   3) original structure needs an enum specifying behavior mode
   4) Template instantiation cannot be used (although automatic 
type deduction works).


  Using these and limiting it seems like it would sort of bring 
back C++'s design of classes, only better, simpler, and more 
powerful. It won't have an actual full virtual table as 
everything is known statically ahead of time, with only minor 
checks by opDispatch to determine which function set to run.

  Issues:
   1) the base/original struct always calls it's own functions. A 
little work and I can probably remove that limitation, but the 
original struct would end up being only storage with no behavior 
at all.
   2) Template instantiation. This is a limitation in opDispatch() 
where calling through it. As shows below. Maybe I'm just using 
opDispatch wrong.

//signatures lacking structs
auto ref opDispatch(string fun, Args ...)(auto ref Args args) 
@property;
I templateType2(string val1, I)(I val);


//call
ps.templateType2!"Extra Extra!"(50);

Error: ps.opDispatch!("templateType2") isn't a template


  Beyond this it seems to have some potential. Thoughts? Ideas?


More information about the Digitalmars-d mailing list