Associative Array & different template types

Robert M. Münch robert.muench at saphirion.com
Wed Jul 3 18:20:18 UTC 2019


I have something like:

template myStruct(T){
	auto makeMyStruct(T,E)(T context, void delegate(E) myFunc){
		static struct myObject {
			this(T context, void delegate(E) myFunc){
				_context = context;
				_myFunc = myFunc;
			}

			private:
				void delegate(E) _myFunc;
				T _context;
		}
	}
}

class myClass {
	void myClassFunc();
}

myClass mc = new myClass;

auto mo = makeMyStruct!myClass(mc, &mc.myClassFunc); // mo = myObject

So, I need to carry around the object from which a delegate was created 
from because it's not possible to query the delegate for the object 
later somewhere else in the code.

Now, I want to keep several of the makeMyStruct!... created objects in 
an AA (or an other container) so that I can compare them with an object 
pointer. Like this (not working code, just pseudo code):


myObject[string] myObjectArray;

auto mo1 = makeMyStruct!myClassA(mcA, &mcA.myClassFunc); // mo1 = myObject
auto mo2 = makeMyStruct!myClassB(mcB, &mcA.myClassFunc); // mo2 = myObject

myObjectArray["1"] = mo1;
myObjectArray["2"] = mo2;

assert(mcA == mo1._context)
assert(mcA == myObjectArray["1"]._context)


I hope the idea is understandable. Of course this doesn't work because 
myObjectArray needs a template, which creates different types. But with 
different types I can't throw everything into on array. May claases, 
and inheritance from a non-template base type help here?

I would like to keep it as simple as possible...

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



More information about the Digitalmars-d-learn mailing list