Variant.type bug ?
Voitech via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Mar 23 07:52:33 PDT 2016
On Wednesday, 23 March 2016 at 12:52:24 UTC, Adam D. Ruppe wrote:
> On Wednesday, 23 March 2016 at 08:01:36 UTC, Voitech wrote:
>> Hi Variant stores variant.type as not the "highest" in
>> hierarchy.
>
> Yeah, it stores the static type. You can use it to get that
> then do a normal dynamic cast to test for a more derived type.
Ok but how to handle sittuation like this ?
class TypeHolder{
import std.variant;
Variant[TypeInfo] data;
void add(T)(T value){
data[typeid(value)]=value;
}
T getByType(T)(){
Variant retVar=data.get(typeid(T),Variant(null));
T val=retVar.get!T; //fails
return val;
}
}
unittest{
import std.variant;
A a= new A;
B b= new B;
C c = new C;
A ab= new B;
A ac = new C;
TypeHolder holder = new TypeHolder;
holder.add(a);
holder.add(ab);
holder.add(ac);
assert(holder.data.length==3);
A result=holder.getByType!A;
assert(result==a);
result=holder.getByType!B; //fails
assert(result==ab);
result=holder.getByType!C; //fails
assert(result==ac);
}
I can hold objects in other AA but Object[TypeInfo] rather than
Variant. Or is there a way to get super type of provided T ?
More information about the Digitalmars-d-learn
mailing list