Thread overview
Templates at runtime
Feb 13, 2009
Justin
Feb 13, 2009
Tim M
Feb 13, 2009
Christopher Wright
Feb 13, 2009
Ary Borenszweig
Feb 14, 2009
Christopher Wright
Feb 15, 2009
Simen Kjaeraas
February 13, 2009
I'm trying to instantiate a templated class at runtime and having some trouble. My thought is that since something like "Collection!(int)" will return a valid classinfo, I ought to be able to use ClassInfo.find() and then .create:

module test;

import std.stdio;

void main() {

	myClass!(int) c = new myClass!(int)();

	string cName = c.classinfo.name;
	writefln(cName);
	ClassInfo ci = ClassInfo.find(cName);
	assert(ci !is null);                                    // Line 12

	Object o = ci.create();
	assert(o !is null);

}
public class myClass(T) { }

This outputs:
test.myClass!(int).myClass
Error: AssertError Failure test(12)

Is there a way to "find" a classinfo for such a class at runtime?
February 13, 2009
On Thu, 12 Feb 2009 21:51:01 -0500, Justin wrote:

> Is there a way to "find" a classinfo for such a class at runtime?

I can't see anything incorrect about what you are doing.  Certainly the source for druntime seems to indicate it should work.

This is probably a compiler bug.

-Steve
February 13, 2009
On Fri, 13 Feb 2009 17:38:46 +1300, Steve Schveighoffer <schveiguy@yahoo.com> wrote:

> On Thu, 12 Feb 2009 21:51:01 -0500, Justin wrote:
>
>> Is there a way to "find" a classinfo for such a class at runtime?
>
> I can't see anything incorrect about what you are doing.  Certainly the
> source for druntime seems to indicate it should work.
>
> This is probably a compiler bug.
>
> -Steve


Probably just not even implemented. A lot of features are just planned and reserved.
February 13, 2009
Justin wrote:
> Is there a way to "find" a classinfo for such a class at runtime?

The slow way:

foreach (modul; ModuleInfo)
	foreach (info; modul.localClasses)
		if (info.name == name) return info;
return null;
February 13, 2009
Christopher Wright wrote:
> Justin wrote:
>> Is there a way to "find" a classinfo for such a class at runtime?
> 
> The slow way:
> 
> foreach (modul; ModuleInfo)
>     foreach (info; modul.localClasses)
>         if (info.name == name) return info;
> return null;

It doesn't work. It seems only top level classes are included in the ModuleInfo.localClasses.

---
module main;

import std.moduleinit;
import std.stdio;

class foo {
}

class myClass(T) {
}

myClass!(int) c;

static this() {
	c = new myClass!(int)();
}

void main() {
	foreach(info; ModuleInfo.modules()) {
		if (info.name != "main") continue;
		
		writefln("%s", info.name);
		foreach(lc; info.localClasses) {
			writefln("  %s", lc.name);
		}
	}
}
---

Output:
main
  main.foo
February 13, 2009
"Christopher Wright" wrote
> Justin wrote:
>> Is there a way to "find" a classinfo for such a class at runtime?
>
> The slow way:
>
> foreach (modul; ModuleInfo)
> foreach (info; modul.localClasses)
> if (info.name == name) return info;
> return null;

If you look at the source for ClassInfo.find, this is exactly what it does. So that probably won't work in this case.

-Steve


February 14, 2009
Ary Borenszweig wrote:
> Christopher Wright wrote:
>> Justin wrote:
>>> Is there a way to "find" a classinfo for such a class at runtime?
>>
>> The slow way:
>>
>> foreach (modul; ModuleInfo)
>>     foreach (info; modul.localClasses)
>>         if (info.name == name) return info;
>> return null;
> 
> It doesn't work. It seems only top level classes are included in the ModuleInfo.localClasses.

Hm. That's odd. Not terribly surprising, I admit; D's runtime reflection is lacking.
February 15, 2009
On Fri, 13 Feb 2009 03:51:01 +0100, Justin <mrjnewt@hotmail.com> wrote:

> I'm trying to instantiate a templated class at runtime and having some trouble. My thought is that since something like "Collection!(int)" will return a valid classinfo, I ought to be able to use ClassInfo.find() and then .create:
>
> module test;
>
> import std.stdio;
>
> void main() {
>
> 	myClass!(int) c = new myClass!(int)();
>
> 	string cName = c.classinfo.name;
> 	writefln(cName);
> 	ClassInfo ci = ClassInfo.find(cName);
> 	assert(ci !is null);                                    // Line 12
>
> 	Object o = ci.create();
> 	assert(o !is null);
>
> }
> public class myClass(T) { }
>
> This outputs:
> test.myClass!(int).myClass
> Error: AssertError Failure test(12)
>
> Is there a way to "find" a classinfo for such a class at runtime?

This is already bugzilla'd. http://d.puremagic.com/issues/show_bug.cgi?id=2484

--
Simen