Multiple modules per file

Dennis dkorpel at gmail.com
Mon Jun 20 15:48:08 UTC 2022


What if you could just concatenate .d files?

You could write a file `pkg.d` that looks like this:
```D
/// Helper module
module pkg.sub;

string name;

/// Main application module
module pkg.app;

import pkg.sub;
void main()
{
     writeln("hello ", name);
}

/// My package
module pkg;

public import pkg.app;
public import pkg.sub;
```

And then this works:
```
dmd pkg.g
```

No new syntax like `module {}` or fancy tricks like 
`mixin("module x;")`, it would be as if the compiler splits up 
the file into multiple files as a preprocessing step.

Other files can only `import pkg`, since the compiler can't find 
e.g. `pkg/sub.d`.

### Why?

It's best practice to keep modules small, but it's not always fun 
to deal with many small files.

In many text editors/IDEs it's easier to scan through a file than 
a file tree.

It's also easier to distribute. Notice how some libraries use 
stand alone files for convenience: 
[arsd](https://github.com/adamdruppe/arsd), 
[stb](https://github.com/nothings/stb), [the SQLite 
Amalgamation](https://www.sqlite.org/amalgamation.html). 
Sometimes, your code *has* to be condensed to one string of text 
for a web IDE, like [codingame](codingame.com/), 
[godbolt](https://d.godbolt.org/), 
[run.dlang.io](https://run.dlang.io/), or when just posting in 
the forums / bugzilla.

And, of course, the recent discussion about `private` to the 
`class`, where putting a `class` in its own file is rejected as a 
solution.

Thoughts?


More information about the Digitalmars-d mailing list