Fastest way to check if the bottom-class of a class reference is any of a set of classes

Per Nordlöw per.nordlow at gmail.com
Mon Dec 16 14:38:59 UTC 2019


What is the fastest way to check whether a class reference is an 
instance of a
bottom equal to any in a set of classes? My current try is 
something like

class C {}

class X : C {}
class Y : C {}
class Z : C {}
...

bool pred(scope const Object object)
{
     return (cast(const(X))object ||
             cast(const(Y))object ||
             cast(const(Z))object ||
             ...);
}

or is it better to switch on the non-scoped (via some existing 
string-to-string
function `unscoped`) part of the `typeid(object).name` like

bool pred(scope const Object object)
{
     const name = typeid(object).name.unscope;
     import std.algorithm.comparison : among;
     return name.among!(X.stringof,
                        Y.stringof,
                        Z.stringof,
                        ...);
}

?


More information about the Digitalmars-d-learn mailing list