Mixin C

Paul Backus snarwin at gmail.com
Fri Mar 8 18:03:47 UTC 2024


On Friday, 8 March 2024 at 03:23:12 UTC, Paul Backus wrote:
> By the way, if you did want to treat a `.c` or `.h` file like 
> its own module, you'd still be able to do so with Mixin C. Just 
> write a simple `.d` wrapper, like this:
>
> ```d
> module libwhatever;
>
> mixin(C) {
>     #include <libwhatever.h>
> }
> ```

This also makes it trivial to apply function attributes to C 
declarations. For example, if you want everything from 
`libwhatever.h` to be `nothrow` and `@nogc`, you just write this:

```d
nothrow @nogc
mixin(C) {
     #include <libwhatever.h>
}
```

There's currently no way to do this with ImportC, and even if 
there were, it would likely require modifying the header file 
(for example, with the `#pragma` suggested in [issue 23812][1]).

[1]: https://issues.dlang.org/show_bug.cgi?id=23812


More information about the dip.ideas mailing list