extern (D)?

Jacob Carlborg doob at me.com
Fri Jan 18 04:53:37 PST 2013


On 2013-01-18 09:09, Rob T wrote:

> I have not yet seen examples or documentation explaining how to separate
> interface and implementation from a class or struct. Are you sure this
> can be done?

Yes, it's supposed to work. Just create a class as you normally would 
and compile it as a library. Then create a di file with the same content 
except for the implementation of the methods are removed. Create an 
application that imports the di file and links with the library.

// foo.d
module foo;

import std.stdio;

class Foo
{
     void foo ()
     {
         writeln("Foo.foo");
     }
}

// foo.di
module foo;

class Foo
{
     void foo ();
}

// main.d
module main;

import foo;

void main ()
{
     auto foo = new Foo;
     foo.foo();
}

$ dmd -lib foo.d
$ rm foo.d
$ mv foo.a libfoo.a
$ dmd main.d -L-lfoo -L-L.
$ ./main
Foo.foo

You can try and remove the linker flags when compiling the application, 
then you'll get undefined symbols:

"_D3foo3Foo7__ClassZ"

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list