Hello Zig #
I’ve read a few bits and blurbs about the programming languauge Zig here and there since its inception. Never took a closer look. Then José Valim recommended the talk Zig <3 Elixir by Isaac Yonemoto. I checked it out and decided to try the language. Here are some random thoughts about getting to know Zig.
I haven’t done much programming suitable for systems, and “lower level” things since before my professional career as a software developer. Back then I really loved C. After C I learned Python, then Haskell and Java. Anyway I wanted to refamiliarize myself with some of the concepts (manual memory management, pointers, etc), and Zig just looked more fun than Rust. There’s a also a pretty good page on Why Zig on the official site, which tbh I didn’t even read until now.
Since Zig is pre 1.0 I knew that this may be a bumpy ride and to expect changes to the language. Yesterday I read about the coming allocator changes for example - Allocgate is coming in Zig 0.9, and you will have to change your code.
I started by installing the latest Zig and going through the Getting started guide. No issues at all on my 2017 Macbook pro.
❯ zig version 0.9.0-dev.1795+aa61e03f2
I’ve wanted to try the Ray tracing in one weekend thing for a while. Using Zig seemed like a pretty good fit!
And I can “just” port the C++ code, and not fret too much on the details if I don’t feel like it maybe?
That turned out to be mostly true. I did have to think and research a bit more when it came to doing polymorphism in Zig. It’s a much more manual ordeal compared to C++. The result is quite nice though. But that was something I felt could be a good language addition, something like interfaces.
My take on the RTIOW, zigray, is available on github: github.com/vorce/zigray/
I decided to not implement the glass material because I don’t think it looks that good to be honest. I want to explore doing some calculations concurrently with async though, that would be cool. Also adding some textures would be neat.
One thing I didn’t have to worry so much about was memory management in this project. All in all zigray felt like a win.
Advent of Code
Okay so the ray tracing experiment went pretty well. Last year’s AoC was fun. I don’t feel like putting as much effort in this year though. So maybe a good trade off is to try it in Zig with a more casual attitude towards the whole thing? Yes why not.
First thing I noticed is that parsing text was hard. And it took me to Day 4 or something to learn about std.mem.split and std.mem.tokenize lol. With those it’s better, but still not great. This seems pretty intentional, and I could probably find some library to help me. My mindset is still not in Zig mode.
I got extremely bored with AoC in Zig already on Day 4(!) (edit: I completed four more days after writing this). Way too much manual housekeeping around the actual problem solving I felt. This feeling I think is because of:
- I’m still much more used to thinking in a FP style, avoiding mutations for example
- Zig n00b, I simply don’t know the best approaches, structures, or the standard library good enough
- I know the solution to the problem, and it’s trivial to implement in a language I know better
- Not pausing and reading more before trying things out
I also had some problems refactoring Zig code, again due to the above I guess. Although the compiler is quite helpful! Documentation on ArrayList and HashMap was sometimes a little lacking. Hard to find examples, I frequently browsed the Zig lang source code, which on one hand was cool and interesting.
Didn’t feel that great about my AoC in Zig sadly.
I like Zig! It’s awesome and fun. And it can become a nice tool for me that I’ve been missing, such as for game development. Practically speaking I think it’s clear that it’s better to learn Rust (if you can only pick one) at the moment.
I have a long way to go until I become comfortable and fluent with the language though.
Looking forward to 1.0!