Is this meant to be possible?
ted
foo at bar.com
Mon Feb 17 16:31:23 PST 2014
I've been using D (at a primitive level) for a while now at work (a large
test-harness that exercises our main code. The harness launches and monitors
multiple processes with multiple threads and performs actions on those
processes to ensure correct behaviour).
I am wanting to stretch my 'D' wings. I'm having some problems with trying
to do the following (reduced case) - and I'm wanting to know if D will even
(ever) allow it. Main reason is that I'm porting 'Ash' - a component/entity
system written in actionscript, which uses this type of construct. The key
issue is that if the line in main() is uncommented - a linker error occurs.
Clearly, there is a 'visibility' issue of 'provider' regarding 'TestClass'.
However, even if I add 'import main:TestClass' into module 'provider' - (and
introduce a compile-time circularity that I absolutely do not want) - it
still causes the link error - I guess the necessary information is lost
through the IProvider interface.
-------------------------------------
module IProvider;
public interface IProvider
{
string providedType();
T createInstance(T)();
}
-------------------------------------
module provider;
import IProvider;
public class Provider(T): IProvider
{
string providedType() { return T.classinfo.stringof;}
public T createInstance(T)() { return new T; }
}
-------------------------------------
module manager;
import provider;
import IProvider;
public class Manager
{
private { IProvider[ClassInfo] mProviders; }
public void add(T)() { mProviders[T.classinfo] = new Provider!T(); }
public IProvider get(T)() {
if ( T.classinfo in mProviders )
return mProviders[ T.classinfo ];
else
return null;
}
}
-------------------------------------
import manager;
import IProvider;
import std.stdio;
void main()
{
auto mgr = new Manager();
mgr.add!TestClass();
IProvider provider = mgr.get!TestClass();
writeln("managed type: ", provider.providedType);
//auto tmp = provider.createInstance!TestClass();
// Linker error if above line is uncommented
}
class TestClass
{
public int value;
}
-------------------------------------
More information about the Digitalmars-d-learn
mailing list