Variant.type bug ?

Voitech via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 23 01:01:36 PDT 2016


Hi Variant stores variant.type as not the "highest" in hierarchy. 
Like this
A a= new A;
A b = new B; //B:A
Variant bVar=Variant(b);
bVar.type will be typeid(A) not typeid(B). Is this intentional ? 
If so is there a way to get "concrete" type of "b" variable like 
when passing to template function ?

void templateFunc(T)(T v){//just test function for B not used 
with other type
	import std.variant;
	typeof(v) val=v;//concrete type ??
	Variant var=val;
	assert(var.type==typeid(B));//fails
}

unittest{
	A b= new B;
	templateFunc(b);
}

Types and unittests:

module typeTest;
import std.traits;
import std.meta;
class A{

	void a(){}
}

class B:A{
	int b(){
		return 1;
	}

}

class C:B,D{
	string c(){
		return "";
	}
	override int d() {
		return 0;		
	}
}

interface D{
	int d();
}

void templateFunc(T)(T v){//just test function for B not used 
with other
	import std.variant;
	typeof(v) val=v;//concrete type ??
	Variant var=val;
	assert(var.type==typeid(B));//fails
}

unittest{
	A b= new B;
	templateFunc(b);
}

unittest{
	import std.variant;
	A a= new A;
	B b= new B;
	C c = new C;

	A ab= new B;
	A ac = new C;

	Variant variant;
	variant=a;
	assert(typeid(a) == variant.type);
	variant=b;
	assert(typeid(b) == variant.type);
	variant=c;
	assert(typeid(c) == variant.type);
	variant=ab;
	assert(typeid(ab) == variant.type); //fails
	variant=ac;
	assert(typeid(ac) == variant.type); //fails
}


More information about the Digitalmars-d-learn mailing list