digitalmars.D - Templates at runtime
- Justin <mrjnewt hotmail.com> Feb 12 2009
- Steve Schveighoffer <schveiguy yahoo.com> Feb 12 2009
- "Tim M" <a b.com> Feb 13 2009
- Christopher Wright <dhasenan gmail.com> Feb 13 2009
- Ary Borenszweig <ary esperanto.org.ar> Feb 13 2009
- Christopher Wright <dhasenan gmail.com> Feb 14 2009
- "Steven Schveighoffer" <schveiguy yahoo.com> Feb 13 2009
- "Simen Kjaeraas" <simen.kjaras gmail.com> Feb 15 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?
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: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
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: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
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
"Christopher Wright" wroteJustin 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
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









"Tim M" <a b.com> 