October 21, 2008
Bruno Medeiros wrote:
> Getting that information at *runtime* might be sensible, but I'm not seeing much usefulness in that.

The main use case I can see is a plugin solution. For example, automatically instantiate all classes that extend IPlugin or something. However, requiring a config file that includes the class name and maybe some metadata isn't too hard to do and is potentially more efficient.
November 01, 2008
Steven Schveighoffer wrote:
> "Sean Kelly" wrote
>> Andrei Alexandrescu wrote:
>>> 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.
>> Much of it is even exposed, just not in a convenient manner.  A list of classes is available via ModuleInfo, and ClassInfo provides a means of finding the ClassInfo instance of a class by name.  From there it should be possible to obtain a list of parent classes, and so one could build a hierarchy graph with some work.
> 
> Sure, but what do you do with that graph ;)  Calling methods/ctors is not exactly easy with D currently.
> 
> -Steve 

Parameterless constructors are easy. Other than that, you can't be certain what expectations the constructor has, so it's a bit dangerous, but you can of course get around that with documentation and try/catch.

Calling methods isn't so difficult, either, since most of the stuff you'll be able to do involves virtual methods from some known base class.

Dunit uses runtime reflection to get all test fixtures and run them. It uses something like:
ClassInfo[] derived(ClassInfo info)
{
	ClassInfo[] derivedClasses;
	foreach (module; ModuleInfo) foreach (clazz; module.localClasses)
	{
		if (isDerived(clazz; info)) derivedClasses ~= clazz;
	}
	return derivedClasses;
}

// Only works for interfaces.
bool isDerived(ClassInfo derived, ClassInfo base)
{
	while (derived)
	{
		if (base is derived) return true;
		derived = derived.base;
	}
}

I did this because I was tired of the static ctor mixin / static singleton registration pattern. I used to have to do:
class FooTests : TestFixture
{
	// I already told you it's a test fixture!
	// You mean I have to tell you twice?!
	mixin (DunitTest);
}

Now I can skip the mixin.
November 01, 2008
Andrei Alexandrescu schrieb:
> 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.
> 
> Andrei

-> Jacha Wetzel
Here is a simple way of adding __traits based runtime reflection to D classes:

http://mainia.de/classinfoex.d
(requires DMD 2.004)
1 2 3
Next ›   Last »