www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is there other way to do that?

reply Jack <jckj33 gmail.com> writes:
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?
Feb 14 2021
next sibling parent evilrat <evilrat666 gmail.com> writes:
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 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 } ```
Feb 15 2021
prev sibling next sibling parent Rumbu <rumbu rumbu.ro> writes:
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.
Feb 15 2021
prev sibling parent mw <mingwu gmail.com> writes:
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.
Feb 15 2021