Problems with Reflection in Static library

Tomek Sowiński just at ask.me
Sat Dec 18 04:07:41 PST 2010


Mandeep Singh Brar napisał:

> I have a class A in module testD as follows
> 
> module testD;
> import std.stdio;
> public class A {
> 	public this() {
> 		writeln("const");
> 	}
> 	public void a(string l, int k) {
> 		writeln("Hello B.a", l, k);
> 	}
> }
> 
> I have another file which uses this class as follows:
> 
> module user;
> import std.stdio;
> 
> int main() {
> 	Object obj = Object.factory("testD.A");
> 	if(obj is null)
> 		writeln("null object");
> 	else
> 		writeln("object created");
> 	return 0;
> }
> 
> When i compile the class A as "dmd -c -of testD testD.d" and the second file as "dmd user.d testD",
> the example works fine and the object gets created.
> 
> But When i compile the class A as "dmd -c -lib testD.d" and the second file as "dmd user.d testD.a",
> the example gives me a null object.
> 
> The Object.factory method does not seem to work if my class has been compiled as a static library.
> Can you please let me know how to solve this. I have tried replacing public with export for class
> testD, but that does not help.

 I think dmd creates type information for each compilation, but doesn't merge it when linking. I hope it's a bug and will also be fixed for dynamic libs.

A (not nice) workaround may be exposing in your lib:

Object objectFactory(string name) {
	return Object.factory(name);
}

and calling it in your program. This should look for the type name in your lib's info.

BTW, am I the only one to think Object.factory is a bad name? It doesn't return a factory. Sure, one can get used to it, but why not Object.make or .create or .instance?

-- 
Tomek



More information about the Digitalmars-d-learn mailing list