Template Collections of Different Types

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Aug 15 18:51:35 PDT 2015


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:
http://stackoverflow.com/questions/31993705/d-call-a-function-using-a-string-variable-with-its-name/31996526#31996526

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.


More information about the Digitalmars-d-learn mailing list