Modules ... "import" vs. "compilation" ... what is the real process here?

Ali Çehreli acehreli at yahoo.com
Tue Sep 28 05:26:29 UTC 2021


On 9/27/21 10:38 AM, james.p.leblanc wrote:

 > I have trouble understanding "module imports" vs. "module compilations".

In addition to what Mike Parker said, templates do complicate matters 
here: Templates are instantiated (i.e. compiled for a specific set of 
template arguments) by modules that actually use those templates.

So, if no non-template definition is used from a module, that module 
need not appear separately on the build line.

For example, 'module a' defines just a function template:

```
module a;

auto doubled(T)(T value) {
   return value * 2;
}
```

The main module imports 'a' and uses and instantiation of a template 
from that module:

```
module main;

import a;

void main() {
   assert(42.doubled == 84);
}
```

The build line may be confusing because in this case a.d need not appear 
on the build line:

$ dmd main.d

Everything works because the compilation of instance doubled!int is 
compiled when compiling main.d and the linker happily finds all symbols 
that are called in the program.

Ali



More information about the Digitalmars-d-learn mailing list