www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Templates at runtime

reply Justin <mrjnewt hotmail.com> writes:
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?
Feb 12 2009
next sibling parent reply Steve Schveighoffer <schveiguy yahoo.com> writes:
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
Feb 12 2009
parent "Tim M" <a b.com> writes:
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.
Feb 13 2009
prev sibling next sibling parent reply Christopher Wright <dhasenan gmail.com> writes:
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;
Feb 13 2009
next sibling parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
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
Feb 13 2009
parent Christopher Wright <dhasenan gmail.com> writes:
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.
Feb 14 2009
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"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
Feb 13 2009
prev sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
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
Feb 15 2009