←back to thread

Zig is hard but worth it

(ratfactor.com)
401 points signa11 | 1 comments | | HN request time: 0.21s | source
Show context
rayiner ◴[] No.36150933[source]
Alternative languages are cool, but I struggle to see the point of a systems programming language that doesn't offer static memory safety in 2023. Rust isn't necessarily the best and final answer--it seems like there is a broad design space to explore for memory-safe systems programming languages. But Zig seems to occupy the same local maximum as C--a relatively simple, non-safe systems language--and doesn't have a killer feature that justifies not just using C.
replies(3): >>36151428 #>>36152916 #>>36155063 #
helen___keller ◴[] No.36151428[source]
If we’re comparing C to Zig I’m not sure what memory safety even needs to be mentioned for.

For C to Zig there’s plenty of reasons one might prefer Zig. For memory safety obviously you might opt to choose neither.

replies(2): >>36151840 #>>36160740 #
rayiner ◴[] No.36151840[source]
In a vacuum, one might prefer Zig. But given that everyone already knows C and the ecosystem is so highly developed, it takes a gamechanging feature like Rust's memory safety to make an alternative language attractive (beyond a "this is cool" project--which I totally support).
replies(2): >>36152648 #>>36152940 #
alwaysbeconsing ◴[] No.36152940[source]
I haven't tried Zig, but "all the things you like about C plus better versions of most/all of the awkward bits" seems like a reasonable value proposition. Especially since the compiler can apparently let you use C painlessly alongside your Zig -- enabling incremental rewriting.
replies(1): >>36153323 #
j-james ◴[] No.36153323[source]
Can you use Zig painlessly alongside C? Does `zig cc` or an equivalent provide for writing Zig libraries that then can be called by a main C function?
replies(2): >>36154394 #>>36154464 #
helen___keller ◴[] No.36154464[source]
Yes probably as painlessly as a nonC language can get. First class support for C calling conventions, struct layouts, and so on

https://ziglearn.org/chapter-4/ See here

replies(1): >>36156173 #
j-james ◴[] No.36156173[source]
Yeah, I read through that: unless I'm missing something I think what I'm curious about is "calling C code from Zig and vice versa" in the to-be-written section.

Nim also has support for `ctypes` and compiles to C as its main target: yet though its interop is powerful it lacks in ergonomics, formerly you had to manually wrap every function you wished to use and this was only recently fixed by a macro-heavy external library.

I'm wondering what Zig does because IMO even if you have an excellent technical solution getting people to actually use it alongside C is hard, it has to be seamlessly seamless. Nim's C interop is rarely used outside of wrappers and it even more rarely is used to produce C libraries (though perhaps that's more a fault of incompatible type systems)

replies(1): >>36156461 #
TUSF ◴[] No.36156461[source]
With Zig, you just write something like:

  const c = @cImport({
      @cDefine("SOME_MACRO", "1");
      @cInclude("raylib.h");
  });
Which translates the header files directly into Zig and allows you to call into them under whatever namespace you assigned them under. You even get completions (assuming you're using the language server)
replies(1): >>36159904 #
j-james ◴[] No.36159904[source]
Right, but I'm curious about the calling-Zig-from-C interop, not the other way around.
replies(1): >>36196814 #
1. kmz42 ◴[] No.36196814[source]
I was curious as well so I made a simple example to see how easy it is[0]

The documentation, especially for the build system, was lacking. But the interop itself is pretty smooth. Only rough point is that I can't define a struct type in Zig and use it in C; you HAVE to define your structs in C if you plan to pass them between the languages (as far as I know anyways).

Zig has documentation on making your Zig structs match the layout of C structs[1] so I assume the intended use case is not as I have done (use the C type in Zig) but instead of define a matching Zig type and cast your C structs into Zig structs, or vice versa, at the boundary between languages.

Structs aside, for functions you just stick "export" on the Zig functions to make them available at link time (presumably) and use "callconv(.C)" so C can call them cleanly. Very easy.

[0] https://github.com/kmz42/zig_c_interop_test

[1] https://ziglearn.org/chapter-4/#extern-structs