Thread overview
Is there other way to do that?
Feb 15, 2021
Jack
Feb 15, 2021
evilrat
Feb 15, 2021
Rumbu
Feb 15, 2021
mw
February 15, 2021
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?
February 15, 2021
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:
>

Non-templated interface/base class is probably the only way I'm aware of, it is also how it is usually done with C# generics where needed for the same reason.

Even though typeid() knows the type I have no idea how to use it that way.


Interface adds 1 pointer to classinfo or vtbl, so it is increases instance size a bit.

```
import std;

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

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

void main()
{
    writeln(__traits(classInstanceSize, AA!void)); // prints 24
    writeln(__traits(classInstanceSize, A!void)); // prints 16
}

```


February 15, 2021
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.

February 15, 2021
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);
>

You have object instance, why not just use virtual function?


https://forum.dlang.org/post/gemozfonxoohojtlbqgj@forum.dlang.org

Cast always smells hacking.