| Thread overview | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
February 13, 2009 Templates at runtime | ||||
|---|---|---|---|---|
| ||||
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 Re: Templates at runtime | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Justin | 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 Re: Templates at runtime | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Steve Schveighoffer | 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 Re: Templates at runtime | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Justin | 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 Re: Templates at runtime | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Christopher Wright | 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 Re: Templates at runtime | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Christopher Wright | "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 Re: Templates at runtime | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Ary Borenszweig | 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 Re: Templates at runtime | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Justin | 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 | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply