4 ms·
> as someone who loves C and dislikes almost every implementation of C interop and FFI I’ve used in other languages (Rust is a notable exception to this), Have
by brabel 2y ago
> as someone who loves C and dislikes almost every implementation of C interop and FFI I’ve used in other languages (Rust is a notable exception to this),
Have you tried D? It can import C files as if they were D modules: https://dlang.org/spec/importc.html https://dlang.org/spec/importc.html
Basically, if there's a `hello.c` file next to your D file, you simply import it with:
import hello
And use the functions it provides. You can also import only a subset of it, of course:
import hello: square;
Or rename the import:
import hi = hello;
The C stdlib is exposed by D's stdlib as if it were a D library: https://dlang.org/phobos/core_stdc_assert_.html https://dlang.org/phobos/core_stdc_assert_.html
C libraries (header files) can be compiled to D and then used as D modules as well, see https://github.com/jacob-carlborg/dstep https://github.com/jacob-carlborg/dstep
Is that as good as Rust?
- jay-barronville 2y ago> Have you tried D? I tried D years ago (at least 5 years ago, I think), but I don’t remember experimenting with the C interop. Your explanation sounds intriguing though. And a couple pretty smart folks I respect have talked about D too. So thanks for bringing it up. I’m going to make some time to experiment with it again some time soon. > Is that as good as Rust? Franky, although I don’t have firsthand experience with D, your explanation and example make the D C interop experience sound actually better than Rust’s. It seems more similar to Zig.
- brabel 2y agoI think the feature that allows importing c, called "importC", is newer than 5 years. I think they may have "copied" it from Zig :D they definitely are trying to not "fall behind" Zig and keep making the language better. With DMD, you can cross compile just as with Zig, but only to object files as the linker is not multi-platform... so I am experimenting with using Zig's linker to "finish" the job. Unfortunately, Zig is not being able to link Phobos, the D stdlib, for reasons I don't understand yet.