Header-Only Library - pragma(root)

Walter Bright newshound2 at digitalmars.com
Sun Jun 14 08:36:50 UTC 2020


There is much misunderstanding in the "What does 'inline' mean?" thread.

I've slowly come to realize that it's not about inlining at all, it's about a 
desire for "header-only" libraries. To use a header-only library, you only have 
to import the module for it.

This works today in D if you also add the imported module to the command line. 
But, of course, it's good to make it simpler.

So, I propose the addition of `pragma(root);` which looks like:

--- electron.di ---
module electron;
pragma(root);    // header-only library!

int orbit() { return 3; }

--- atom.d ---
module atom;
import core.stdc.stdio;

import electron;

void main() {
     printf("%d orbits\n", orbit());
}
--------------

To compile, just:

   dmd atom

and the pragma(root) will cause the compiler to behave as if you'd typed:

   dmd atom electron

Not only does this make for header-only libraries, but if you have a project 
that is split up into several modules, it becomes super convenient to compile it 
if the other modules are all annotated with pragma(root).

Note that there's no "inline" anywhere. Inlining will remain about inlining, and 
nothing else.


More information about the Digitalmars-d mailing list