digitalmars.D - Templates at runtime
- Justin (17/17) Feb 12 2009 I'm trying to instantiate a templated class at runtime and having some t...
- Steve Schveighoffer (5/6) Feb 12 2009 I can't see anything incorrect about what you are doing. Certainly the
- Tim M (4/10) Feb 13 2009 Probably just not even implemented. A lot of features are just planned a...
- Christopher Wright (6/7) Feb 13 2009 The slow way:
- Ary Borenszweig (29/38) Feb 13 2009 It doesn't work. It seems only top level classes are included in the
- Christopher Wright (3/16) Feb 14 2009 Hm. That's odd. Not terribly surprising, I admit; D's runtime reflection...
- Steven Schveighoffer (4/11) Feb 13 2009 If you look at the source for ClassInfo.find, this is exactly what it do...
- Simen Kjaeraas (4/24) Feb 15 2009 This is already bugzilla'd. http://d.puremagic.com/issues/show_bug.cgi?i...
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
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
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:Probably just not even implemented. A lot of features are just planned and reserved.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 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;
Feb 13 2009
Christopher Wright wrote:Justin wrote: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.fooIs 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
Ary Borenszweig wrote:Christopher Wright wrote:Hm. That's odd. Not terribly surprising, I admit; D's runtime reflection is lacking.Justin wrote:It doesn't work. It seems only top level classes are included in the ModuleInfo.localClasses.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 14 2009
"Christopher Wright" wroteJustin wrote:If you look at the source for ClassInfo.find, this is exactly what it does. So that probably won't work in this case. -SteveIs 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
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