d coder <dlang.co...@gmail.com> wrote:

Greetings

I need a way to know (using traits or other compile time constructs)
all the types derived from a given type.
Is it possible in D?

No.

Is it possible to get a list of all the user-defined classes? I could
use that to filter out the classes that I need.

No.


However, it is possible at run-time to iterate through all classes and
at that time compare them to the base class:

T[] getDerivedClasses( T )( ) if ( is( T == class ) ) {
    T[] result;
    foreach ( mod; ModuleInfo ) {
        foreach ( cls; mod.localClasses ) {
            if ( mod.name == "object" ) {
                break;
                }
            auto cls_base = cls;
            do {
                if ( cls_base.name == T.classinfo.name ) {
                    if ( T tmp = cast(T)cls.create( ) ) {
                        result ~= tmp;
                    }
                }
            } while ( ( cls_base = cls_base.base ) != Object.classinfo );
        }
    }
    return result;
}


--
Simen

Reply via email to