On Wednesday, 30 September 2015 at 18:01:57 UTC, Adam D. Ruppe
wrote:
On Wednesday, 30 September 2015 at 17:51:50 UTC, Jan Johansson
wrote:
The interface file (I called it test.di):
FYI there's no actual difference between .di files and .d
files. The way D is meant to be used is you write the
declarations together, then if you want to, you can
automatically strip the bodies out of a .d file (dmd -H does
it) and that makes the .di file.
But you always substitute the .di file for the .d file with the
same name. They cannot be used together like a .h and .cpp file.
You can put an interface in a separate module than a class.
Then you import the interface module from both locations,
though your factory function won't work like that.
Try something like:
itest.d:
module itest;
interface IMyTest {
void Write(string message);
}
test.d:
public import itest; // import the interface definition too
import std.stdio;
private class MyTest : IMyTest {
void Write(string message) {
writeln(message);
}
}
public IMyTest createInstance() {
return new MyTest;
}
main.d:
import test;
import std.stdio;
void main() {
auto p = createInstance();
p.Write("Hello, World!");
}
And that should work.
dmd test.d test.di -lib -oftest
would be more like `dmd itest.d test.d -lib -oftest`
dmd main.d test.di test.a
and `dmd main.d itest.d test.a`
Or you could just compile it all at once with `dmd main.d
test.d itest.d`
Thanks,
But (there is always a but) ;-)
The main.d should rely on itest.d, not test.d, otherwise I do the
declaration for the library itself, but the main.d includes the
test.d - the implementation (not the declaration).
If I change the 'dmd main.d test.d test.a' to 'dmd main.d itest.d
test.a', then I got a new error: itest.d:(.text._Dmain+0x5):
undefined reference to `_D5itest14createInstanceFZC5itest7IMyTest'
The linker get confused about the separation of the declaration
and implementation.
Best regards
Jan Johansson