digitalmars.D.learn - Template Collections of Different Types
- DarthCthulhu (15/15) Aug 15 2015 Say I want to do something like:
- Adam D. Ruppe (16/23) Aug 15 2015 You don't, not like that anyway. The attach call is the ruin of
- DarthCthulhu (8/21) Aug 15 2015 I see. Yeah, I thought that might be the case. Nice to know I
- BBasile (22/38) Aug 15 2015 You would need a kind of type info system:
- Adam D. Ruppe (4/8) Aug 15 2015 You could also just use D's own run time typeinfo with the
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
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
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:I see. Yeah, I thought that might be the case. Nice to know I wasn't just being ignorant.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.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
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
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