On Wednesday, 30 September 2015 at 17:51:50 UTC, Jan Johansson
wrote:
Hello all,
I'm testing D language, and the first thing I want to do is to
separate declaration from implementation. I was happy to find
support of interfaces in the D language, and set out to do a
basic test. However, this test failed, and I want some newbie
help to understand how it should be done in D language.
----------------------
The interface file (I called it test.di):
// Interface
interface IMyTest {
void Write(string message);
}
// Factory for type implementing IMyTest
IMyTest createInstance();
----------------------
The library file (I called it test.d):
import std.stdio;
class MyTest : IMyTest {
void Write(string message) {
writeln(message);
}
}
IMyTest createInstance() {
return new MyTest;
}
----------------------
And finally the main file (I called it main.d):
import test;
import std.stdio;
void main() {
auto p = createInstance();
p.Write("Hello, World!");
}
----------------------
The assumption was that I could do:
dmd test.d test.di -lib -oftest
and next do:
dmd main.d test.di test.a
The shared information is in the test.di file.
However, this failed, since the first statement generates the
following:
dmd test.d test.di -lib -oftest
Error: module test from file test.di conflicts with another
module test from file test.d
I guess it is because the file name test.d and test.di creates
conflict, surfaced as module test.
How can I accomplish what I want to do?
Kind regards,
Jan Johansson
Hi again,
Here is a re-worked example. Again, I want to use the factory
pattern, and have created four files.
---
test.d (interface):
interface IMyTest
{
void Write(string message);
}
---
mytesting.d (one implementation of interface):
import test;
class MyTesting : IMyTest {
void Write(string message) {
import std.stdio;
writeln("MyTesting: " ~ message);
}
}
---
factory.d (factory):
import test;
import mytesting;
IMyTest createInstance() {
return new MyTesting;
}
---
main.d (program):
import factory;
import std.stdio;
void main() {
auto p = createInstance();
p.Write("Hello, World!");
}
---
There are a number of ways to build the executable.
I want to use binary linkage as much as possible (the next step
is to do dynamically linkage - but here I need help - I'm still a
newbie).
First I build the component (class) artifact:
dmd mytesting.d -lib -ofmytesting
Next I build the factory artifact:
dmd factory.d -lib -offactory
And then I close the loop:
dmd main.d test.d factory.a mytesting.a
---
//Jan