www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - TypeInfo for highest type in hierarhy (class) when passed base type

reply Voitech <woipoi gmail.com> writes:
How to handle this situation:
module typeTest;

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();
}
TypeInfo __typeInfo(T)(T t){
	return typeid(T);
}

unittest{
	A a= new A;
	A b= new B;
	A c = new C;

	TypeInfo aInfo=__typeInfo(a);
	TypeInfo bInfo=__typeInfo(b);
	TypeInfo cInfo=__typeInfo(c);

	assert(aInfo!=bInfo); //fails
	assert(aInfo!=cInfo);//fails
	assert(bInfo!=cInfo);//fails

}
Mar 16 2016
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 03/16/2016 01:03 AM, Voitech wrote:
 How to handle this situation:
 module typeTest;

 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();
 }
 TypeInfo __typeInfo(T)(T t){
      return typeid(T);
typeid() can take types and expressions. You need to replace T with t because T is always A in your tests. This works: typeid(t); Ali
 }

 unittest{
      A a= new A;
      A b= new B;
      A c = new C;

      TypeInfo aInfo=__typeInfo(a);
      TypeInfo bInfo=__typeInfo(b);
      TypeInfo cInfo=__typeInfo(c);

      assert(aInfo!=bInfo); //fails
      assert(aInfo!=cInfo);//fails
      assert(bInfo!=cInfo);//fails

 }
Mar 16 2016