how to implement a function in a different D source file

Elronnd elronnd at elronnd.net
Tue Nov 26 05:16:35 UTC 2019


On Tuesday, 26 November 2019 at 03:06:52 UTC, Omar wrote:
> the page here https://dlang.org/spec/function.html
> suggests you can implement a function in a different file, and 
> a different tutorial somewhere else mentioned the endeavour of 
> no-bodied-functions as a way of presenting a black-box type of 
> interface.

This is not a common pattern in d; the only reason it's used in c 
and c++ is that those languages don't have a real module system.  
However, the way it's done is with .di (d interface) files.  
Consider:

test.di:
module test;

void print_stuff();

main.d:
import test;

void main() {
         print_stuff();
}


test.d:
module test;
void print_stuff() {
         import std.stdio;
         writeln("I'm stuff");
}


You can verify the compiler is reading from test.di by putting 
test.d in a different directory from main.d and test.di.


More information about the Digitalmars-d-learn mailing list