How do I find the actual types of the elements in a list of classes?

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 13 13:28:31 PDT 2015


On Thursday, 13 August 2015 at 20:23:56 UTC, Jack Stouffer wrote:
> As far as I can tell, there is no way to know the actual type 
> of each of the objects in the list to be able to print:

Cast it to Object first, then do the typeid and it will get the 
dynamic class type. Since Parent is an interface, typeid works 
differently.

I wrote about this in more detail recently here:
http://stackoverflow.com/questions/31563999/how-to-get-classinfo-of-object-declared-as-an-interface-type/31564253#31564253

it is a bit of a FAQ, but there's a solid reason behind the 
behavior.


> Also, this fails to compile when it doesn't look like it should:

I believe that's a well-known bug, the array literal tries to 
type it all to the first element instead of looking for the 
common types of all elements.

You could explicitly cast to the interface if you needed to, I 
believe just casting the first one will cause it to do the rest 
automatically.


More information about the Digitalmars-d-learn mailing list