Add modules to C with 10 lines of code

Paul Backus snarwin at
Sun Jan 16 14:55:40 UTC 2022

On Sunday, 16 January 2022 at 09:47:00 UTC, Walter Bright wrote:
> Yes, real, live modules!
> (I can hardly believe this works!)

What's the intended use-case for this? If I'm using ImportC to 
import the header file for an existing C library, it's not going 
to contain a D-specific feature like this. If I'm writing a new 
library, and I want it to be usable from C, I can't use __import 
in the header file, because my C compiler won't recognize it. And 
if I only want my library to be usable from D, then I can just 
write a D module, and use a normal D import statement.

I guess it makes it possible to write code like this:

     /* example.h */
     #ifdef __IMPORTC__
     __import code.stdc.stdio;
     #include <stdio.h>

     void example(void) { puts("example"); }

...but I'm struggling to see what advantages an example like the 
above has compared to the status quo. Slightly faster compile 
times, because __import is faster to process than #include? Less 
namespace pollution, because imported symbols aren't visible 
outside of the C header but #included symbols are?

More information about the Digitalmars-d mailing list