←back to thread

Open-source Zig book

(www.zigbook.net)
692 points rudedogg | 3 comments | | HN request time: 0.395s | source
Show context
charlie90 ◴[] No.45950098[source]
>Learning Zig is not just about adding a language to your resume. It is about fundamentally changing how you think about software.

Zig is just C with a marketing push. Most developers already know C.

replies(3): >>45950188 #>>45950368 #>>45951532 #
keyle ◴[] No.45950368[source]
That tagline unfortunately turned me off the book, without even starting to read.

I really don't need this kind of self-enlightenment rubbish.

What if I read the whole book and felt no change?

I think I understand SoA just fine.

replies(1): >>45950420 #
xeonmc ◴[] No.45950420[source]
It is also just such a supremely unziglike thing to state.
replies(1): >>45950587 #
Zambyte ◴[] No.45950587[source]
Early talks by Andrew explicitly leaned into the notion that "software can be perfect", which is a deviation from how most programmers view software development.

Zig also encourages you to "think like a computer" (also an explicit goal stated by Andrew) even more than C does on modern machines, given things like real vectors instead of relying on auto vectorization, the lack of a standard global allocator, and the lack of implicit buffering on standard io functions.

I would definitely put Zig on the list of languages that made me think about programming differently.

replies(2): >>45951107 #>>45951841 #
1. keyle ◴[] No.45951107[source]
I'm not sure how what you stated is different from writing highly performance C.
replies(2): >>45951359 #>>45974632 #
2. budro ◴[] No.45951359[source]
I think it mostly comes down to the standard library guiding you down this path explicitly. The C stdlib is quite outdated and is full of bad design that affects both performance and ergonomics. It certainly doesn't guide you down the path of smart design.

Zig _the language_ barely does any of the heavy lifting on this front. The allocator and io stories are both just stdlib interfaces. Really the language just exists to facilitate the great toolchain and stdlib. From my experience the stdlib seems to make all the right choices, and the only time it doesn't is when the API was quickly created to get things working, but hasn't been revisited since.

A great case study of the stdlib being almost perfect is SinglyLinkedList [1]. Many other languages implement it as a container, but Zig has opted to implement it as an intrusively embedded element. This might confuse a beginner who would expect SinglyLinkedList(T) instead, but it has implications surrounding allocation and it turns out that embedding it gives you a more powerful API. And of course all operations are defined with performance in mind. prepend is given to you since it's cheap, but if you want postpend you have to implement it yourself (it's a one liner, but clearly more expensive to the reader).

Little decisions add up to make the language feel great to use and genuinely impressive for learning new things.

[1] https://ziglang.org/documentation/master/std/#std.SinglyLink...

3. Zambyte ◴[] No.45974632[source]
Consider the following Zig:

    const a = @Vector(4, i32){ 1, 2, 3, 4 };
    const b = @Vector(4, i32){ 5, 6, 7, 8 };

    const c = a + b;
This compiles to this x86-64 code:

    vmovdqa xmm0, xmmword ptr [rip + .LCPI5_0]
    vmovdqa xmmword ptr [rbp - 48], xmm0
    vmovdqa xmm0, xmmword ptr [rip + .LCPI5_1]
    vmovdqa xmmword ptr [rbp - 32], xmm0
    vmovdqa xmm0, xmmword ptr [rip + .LCPI5_2]
    vmovdqa xmmword ptr [rbp - 16], xmm0
C does not provide vector primitive to expose the vector primitives in modern machines. C compilers rely on analyzing loops to see when auto-vectorization is applicable. Auto-vectorization is a higher level of abstraction than directly exposing vector primitives.

Regarding the lack of a standard global allocator, and the lack of implicit buffering on standard io functions, these are simply features of the Zig standard library which are true of computers (computers do not have a standard global allocator nor do they implicitly buffer IO) but are not features of the C standard library, and therefore are not encouraged to use custom allocators or explicit buffering.