www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to get the implementer of an interface?

reply Qian Xu <quian.xu stud.tu-ilmenau.de> writes:
Hello All,

how to get the implementer of an interface?

Here is an example:
-------------------------------------------------------------
interface intf_1 {}
class c_1 : intf_1 {}
class c_2 : c_1 {}

c_1 aaa = new c_1;
c_2 bbb = new c_2;
auto list = [cast(intf_1)(bbb), cast(intf_1)(aaa)];
foreach (intf_1 i; list)
{
  print_intf_implementor(i);
  // bbb should return "c_2"
  // aaa should return "c_1"
}
-------------------------------------------------------------


--Qian
Jan 22 2009
next sibling parent Daniel Keep <daniel.keep.lists gmail.com> writes:
Qian Xu wrote:
 Hello All,
 
 how to get the implementer of an interface?
 
 Here is an example:
 -------------------------------------------------------------
 interface intf_1 {}
 class c_1 : intf_1 {}
 class c_2 : c_1 {}
 
 c_1 aaa = new c_1;
 c_2 bbb = new c_2;
 auto list = [cast(intf_1)(bbb), cast(intf_1)(aaa)];
 foreach (intf_1 i; list)
 {
   print_intf_implementor(i);
   // bbb should return "c_2"
   // aaa should return "c_1"
 }
 -------------------------------------------------------------
 
 
 --Qian
I don't think you can. i.classinfo returns intf_1's ClassInfo. Also, the only two bits of hidden information on an object are the monitor object and the vtable, so I don't know that you could use those, either. Of course, I could be wrong. -- Daniel
Jan 22 2009
prev sibling parent reply "Denis Koroskin" <2korden gmail.com> writes:
On Thu, 22 Jan 2009 11:17:20 +0300, Qian Xu <quian.xu stud.tu-ilmenau.de> wrote:

 Hello All,

 how to get the implementer of an interface?

 Here is an example:
 -------------------------------------------------------------
 interface intf_1 {}
 class c_1 : intf_1 {}
 class c_2 : c_1 {}

 c_1 aaa = new c_1;
 c_2 bbb = new c_2;
 auto list = [cast(intf_1)(bbb), cast(intf_1)(aaa)];
 foreach (intf_1 i; list)
 {
   print_intf_implementor(i);
   // bbb should return "c_2"
   // aaa should return "c_1"
 }
 -------------------------------------------------------------


 --Qian
Try the following: writefln(cast(Object)i);
Jan 22 2009
parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Denis Koroskin wrote:
 On Thu, 22 Jan 2009 11:17:20 +0300, Qian Xu 
 <quian.xu stud.tu-ilmenau.de> wrote:
 
 how to get the implementer of an interface?
[snip]
 
 Try the following:
 
 writefln(cast(Object)i);
Or writefln((cast(Object)i).classinfo.name); if you want the class name, not the result of toString (which is only the class name if it hasn't been overridden).
Jan 22 2009