Getting derived classes

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri Oct 17 08:05:57 PDT 2008


Steven Schveighoffer wrote:
> "Ary Borenszweig" wrote
>> Steven Schveighoffer wrote:
>>> "Ary Borenszweig" wrote
>>>> Andrei Alexandrescu escribió:
>>>>> dsimcha wrote:
>>>>>> I know that, in std.traits, there's a template that spits out all base 
>>>>>> classes
>>>>>> for a given class.  Based on reading the source, it seems to work 
>>>>>> based on a
>>>>>> rather interesting use case for is expressions.  Is there any 
>>>>>> equivalent way
>>>>>> to do the opposite:  For any given position in a class hierarchy, to 
>>>>>> get a
>>>>>> tuple of all possible descendants?
>>>>> That's not possible in general because in D the derived classes form an 
>>>>> open set. I guess it could be done at runtime via reflection (not 
>>>>> implemented afaik), but not at compile time.
>>>> But for a given set of modules, the hierarchy is finite and known.
>>> Not at compile time, only at link time.  You can't compile code at link 
>>> time.
>> I still don't understand why you can't do this at compile time, assuming 
>> you have the source code for everything that you'll link into your 
>> executable.
> 
> Let's say this feature theoretically existed.  Here's an example usage:
> 
> class X
> {
>    Tuple!(???) getAllDerivedTypes() { return std.getAllDerivedTypes!(X)();}
> }
> 
> If you compile this module like:
> 
> dmd -c X.d;
> 
> At this point, how does the compiler know all the modules that will derive 
> from X?

Well explained.

> D's compile-time reflection is generally limited to what is at or below a 
> certain point in the hierarchy, not what has derived from it.

s/below/above/

> If you wanted to do something at runtime, that is possible, you might even 
> be able to do this today by looking at the global TypeInfo array.
> 
> But you certainly cannot get a tuple of the derived classes at compile-time.
> 
>> How is a class hierarchy different in D than in Java? Eclipse JDT's has a 
>> type hierarchy feature, and Bruno was going to implement it in Mrnmhrm, 
>> all based on what's defined in the modules (you can say, compilte-time). 
>> What's the problem?
> 
> Java has runtime reflection.  D does not.  You have to implement runtime 
> reflection on top of compile time reflection (or so says Walter).

I said so. All derived classes is, however, a case of runtime-available
information that's not there during compilation.

Anyhow, there's a start with the function static Object.factory(string).
You give it a string, it gives you a newly-instanced object. So the
information is there, it just needs exposing.


Andrei




More information about the Digitalmars-d mailing list