Hacker News new | ask | show | jobs
by pyrolistical 493 days ago
Since c abi powers the world, with zig’s ability to easily wrap c libs, not only does zig have access to all libs; it also makes it easy to integrate zig with any existing project as most programming languages have integration with c.

This is why everybody says just use some existing c lib

1 comments

Hmm. I could substitute lots of languages for zig in that first sentence. But that doesn't invariably lead the communities that maintain those languages to utter the second.

I also doubt I have space to enumerate the languages that claim to "easily wrap c libs." None of them easily do that. That statement imagines that there's some basic consistency between APIs (and that those APIs are asking for and returning fairly simple types).

The thing is, all valid C types are also valid in Zig. You just `@cImport` the header file and use it. You don't need to wrap the API, you just use it.
I would argue zig had the best c integration. This is why it’s different.
Better than C++? Better than Lua?
arguably Zig's c integration is better than c's c integration.
How so?

(If you're hearing a combative tone, it's unintended; I'm asking seriously, because I would love for this to be true!)

In C, C imports are in the global namespace. The language is also compiled sequentially, so often, time is spent ensuring headers are included in the proper order, hopefully avoiding a cyclic dependency. In Zig, C imports are done in a struct with a local namespace or imported package definition and the ordering is not relevant to compilation.

I think parent means that Zig is a better language with regards to being the target for C to be imported into than C and that is the benefit of being a more modern language with flexibility following more modern conventions. This is combined with the mostly seamless ability to utilize most C functions and types without too much strife.

no combative tone detected.

for example if two c libs declare a function with the same name youre not gonna have a good time using them both from c. I believe that's not a problem in zig.