www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Template Collections of Different Types

reply "DarthCthulhu" <spam spam.com> writes:
Say I want to do something like:

Propertery!int pi = 42;
PropertyCollection pc;

pc.attach("Life_and_Everything", pi);

assert(pc.Life_and_Everything == 42);

Property!string ps = "Hello World";

pc.attach("text", ps);

assert(pc.text == "Hello World");

How would one store the Property objects in the 
PropertyCollection? You can't use something like an array because 
the two Properties are of different types. Do you really need to 
do something like make a member of PropertyCollection for every 
type of Property you are interested in storing and using static 
ifs to determine which variable it goes into?

I feel like there is an obvious solution here that I'm missing.
Aug 15 2015
next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Sunday, 16 August 2015 at 01:39:54 UTC, DarthCthulhu wrote:
 How would one store the Property objects in the 
 PropertyCollection?
You don't, not like that anyway. The attach call is the ruin of it. If it was all one definition, you could use something like std.typecons.Tuple, but multiple calls to attach need to be a runtime function, which means what you attach will lose the compile time type when you get it. Best you could do is something like pc.text!string which checks at runtime and throws if you asked for the wrong type, which is basically just a Variant[string].
 You can't use something like an array because the two 
 Properties are of different types. Do you really need to do 
 something like make a member of PropertyCollection for every 
 type of Property you are interested in storing and using static 
 ifs to determine which variable it goes into?
That would work. You could write a plain struct definition and then the attach could be implemented as a allMembers/getMember assignment loop, similarly to this: but looking at data member assignments instead of method calls. The attach would still check the type with a throw at runtime, since the member name you use is also a runtime thing.
Aug 15 2015
parent "DarthCthulhu" <spam spam.com> writes:
On Sunday, 16 August 2015 at 01:51:36 UTC, Adam D. Ruppe wrote:
 On Sunday, 16 August 2015 at 01:39:54 UTC, DarthCthulhu wrote:
 How would one store the Property objects in the 
 PropertyCollection?
You don't, not like that anyway. The attach call is the ruin of it. If it was all one definition, you could use something like std.typecons.Tuple, but multiple calls to attach need to be a runtime function, which means what you attach will lose the compile time type when you get it.
I see. Yeah, I thought that might be the case. Nice to know I wasn't just being ignorant.
 That would work. You could write a plain struct definition and 
 then the attach could be implemented as a allMembers/getMember 
 assignment loop, similarly to this:


 but looking at data member assignments instead of method calls.
Oh, neat! Thanks! Incidentally, I watched your dconf2015 talk "Dynamic Types in D". That (along with Andrei's "Generic Programming Must Go") actually inspired me to think about this particular problem. It's very appropriate that you would have the solution, then!
Aug 15 2015
prev sibling parent reply "BBasile" <bb.temp gmx.com> writes:
On Sunday, 16 August 2015 at 01:39:54 UTC, DarthCthulhu wrote:
 Say I want to do something like:

 Propertery!int pi = 42;
 PropertyCollection pc;

 pc.attach("Life_and_Everything", pi);

 assert(pc.Life_and_Everything == 42);

 Property!string ps = "Hello World";

 pc.attach("text", ps);

 assert(pc.text == "Hello World");

 How would one store the Property objects in the 
 PropertyCollection? You can't use something like an array 
 because the two Properties are of different types. Do you 
 really need to do something like make a member of 
 PropertyCollection for every type of Property you are 
 interested in storing and using static ifs to determine which 
 variable it goes into?

 I feel like there is an obvious solution here that I'm missing.
You would need a kind of type info system: --- enum Ti {tibyte, tiubyte, ...} Ti getTypeInfo(T)(T t){statif is(t == ubyte) return Ti.tibyte; else...} pc.attach("Life_and_Everything", pi, getTypeInfo(pi)); pc.attach("BlaBlaBla", pi, getTypeInfo(pi)); pc.attach("zkfozekf", 42, getTypeInfo(42)); --- internally the container would store 3 infos (string, pointer to data and a Ti) and when you query a property from its identifier, it looks for the attached type info so that you can cast the result (let's say it would return a pointer). --- TypeInfo getTypeInfo(string identifier); void* getValue(string identifier); --- These kind of mechanism are really common in languages that have a poor/none compile time reflection features, but in D you would have to build your own run time type info system. My example is a bit shitty but you should get the idea.
Aug 15 2015
parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Sunday, 16 August 2015 at 02:42:18 UTC, BBasile wrote:
 ---
 enum Ti {tibyte, tiubyte, ...}
 Ti getTypeInfo(T)(T t){statif is(t == ubyte) return Ti.tibyte; 
 else...}
You could also just use D's own run time typeinfo with the typeid() thing. It returns an instance of class TypeInfo. (This is what std.variant does btw)
Aug 15 2015