Interface file

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Wed Sep 30 12:17:44 PDT 2015


On 9/30/15 2:12 PM, Jan Johansson wrote:

> 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.

There is no reason to use interfaces here, you can separate declaration 
from implementation without them:

test.di:

module test;
class MyTest {
    void Write(string message);
}

test.d:

module test;

class MyTest {
    void Write(string message) {
       writeln(message);
    }
}


main.d:

import test; // will work even if only test.di is available
import std.stdio;

void main() {
    auto p = new MyTest;
    p.Write("Hello, World!");
}

Interfaces are only necessary when you are unsure what concrete type you 
need until runtime.

-Steve


More information about the Digitalmars-d mailing list