www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - List of derived types?

reply d coder <dlang.coder gmail.com> writes:
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?

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.

Regards
Cherry
Dec 16 2010
next sibling parent Michal Minich <michal.minich gmail.com> writes:
V Thu, 16 Dec 2010 18:46:41 +0530, d coder 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?
 
 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.
 
 Regards
 Cherry
you can iterate all modules to find base classes. see object_.d file to see sturcture of TypeInfo_Class or ModuleInfo. foreach(m; ModuleInfo) foreach (c; m.localClasses) if (c.base !is null) writefln(c.base.name);
Dec 16 2010
prev sibling next sibling parent reply =?ISO-8859-1?Q?Pelle_M=E5nsson?= <pelle.mansson gmail.com> writes:
On 12/16/2010 02:16 PM, d coder 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?

 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.

 Regards
 Cherry
I'm curious, why do you need that?
Dec 16 2010
parent d coder <dlang.coder gmail.com> writes:
 I'm curious, why do you need that?
It is a long story. But basically I am creating a platform wherein the users would be deriving classes from some base classes that I write as part of the platform. And the users would often be deriving many such classes. The end-users would often not be programmers and would know little D (as you can see I am also learning D :-). I want to automate some code generation for them and I hope to do that by creating wrapper classes that would shadow the classes that the end-users have written. Since the and users would be instantiating these classes only inside a particular class scope, I wanted to create some code inside that class scope. Right now I am forcing the end-users to insert some mixin for every class that they code. I wanted to automate that process. Hope what I said makes sense to you. Regards Cherry Hope what I said makes sense.
Dec 16 2010
prev sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
d coder <dlang.coder 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
Dec 16 2010