Interface file
Adam D. Ruppe via Digitalmars-d
digitalmars-d at puremagic.com
Wed Sep 30 11:01:55 PDT 2015
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`
More information about the Digitalmars-d
mailing list