Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
December 16, 2010 List of derived types? | ||||
---|---|---|---|---|
| ||||
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 |
December 16, 2010 Re: List of derived types? | ||||
---|---|---|---|---|
| ||||
Posted in reply to d coder | 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);
|
December 16, 2010 Re: List of derived types? | ||||
---|---|---|---|---|
| ||||
Posted in reply to d coder | 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?
|
December 16, 2010 Re: List of derived types? | ||||
---|---|---|---|---|
| ||||
Posted in reply to d coder | 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 |
December 16, 2010 Re: List of derived types? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pelle Månsson | > 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.
|
Copyright © 1999-2021 by the D Language Foundation