On Monday, 15 February 2021 at 07:26:56 UTC, Jack wrote:
I need to check if an instance is of a specific type derived from my base class but this class has template parameter and this type isn't available at time I'm checking it. Something like:

class B { }
class A(T) : B { }
class X : B { }
class Z : B { }

auto c = can be any derived class from B
bool isX = cast(A!???) c !is null;
assert(isX);

an interface would work:

interface IA { }
class A(T) : B, IA { }

bool isX = cast(IA) c !is null;
assert(isX);

but I would have create one just for that. It feels a bit hacky? also, does an empty interface like that increase the A class memory size at all?

Hackier than hack:

bool isA(Object o)
{
    return o && (cast(TypeInfo_Class)(typeid(o))).name == "A";
}

Of course, this is not recommended, I am sure that there is another way to achieve what you want but with other design. Unfortunately, your examples are too limited to have an idea.

Reply via email to