how to implement a function in a different D source file
Mike Parker
aldacron at gmail.com
Tue Nov 26 05:11:21 UTC 2019
On Tuesday, 26 November 2019 at 03:55:24 UTC, mipri wrote:
> 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
> ...
>> mentioned the endeavour of no-bodied-functions as a way of
>> presenting a black-box type of interface.
>
> oh, that's what you were asking.
>
> Consider:
>
> $ cat interface/references.d
> module references;
>
> string greeting();
>
> $ cat implementation/references.d
> module references;
>
> string greeting() {
> return "Hello, Dave";
> }
>
> $ cat main.d
> import references;
>
> void main() {
> import std.stdio: writeln;
> writeln(greeting);
> }
>
> And trying to build it:
>
> $ dmd -c implementation/references.d
> $ dmd -Iinterface -c main.d
> $ dmd main.o references.o
> $ ./main
> Hello, Dave
The idiomatic way to do that is to put the interface in a "D
interface" file (.di) and the implementation in a .d file:
foo/bar.di
foo/bar.d
Then, when importing foo.bar, the compiler will pick up the .di
file automatically.
And you can actually have the compiler generate the interface
file for you from a source file with the -H option.
More information about the Digitalmars-d-learn
mailing list