I am working on a small protobuf decoder & encoder for Zig that uses comptime to generate encoding/decoding functions and I wanted to share some observations. At this point, I wrote a few hundred lines of Zig so take this with a grain of salt.

I found it hard to debug things erroring out at comptime. @compileError() and @compileLog() builtins were a huge help - you can print anything, anywhere to understand the flow of the code better. printf debugging really is king. If you imagine Laslow’s triangle of needs in terms of debugging then printf debugging should be really at the foundational level.

I also found myself yearning to have a better way of catching cases where one type is a subtype (if we can call it that way) of another. For example, std.ArrayList is a function that takes a type and returns another opaque type. It’s not possible to easily say if (T isSubTypeOf std.ArrayList) - instead you have to reach for @hasField, @FieldType, @typeInfo and encode that you are searching for the type of items field during compilation. But, of course, if it moves like a duck, if it quacks like a duck then it probably is a duck. So, if some type has those fields then it probably is a std.ArrayList. I think that on one hand it is extremely powerful but on the hand I would love to have some more elegant way of handling this.

The error handling where you can just make constants and return them, and the way this is encoded in the function’s type signature is just lovely. I can just glance at something and see what kind of errors the function could return most of the time. Kind of like Go’s error type but instead of arbitrary strings, you have constants, typically a predefined set.

The way Zig doesn’t hide any allocation from you is also very lovely. I am a fan of that. I found that especially in Go it is really easy to write some code that accidentally allocates. The language doesn’t make it so clear that an allocation is happening. I am personally a fan of TigerbeetleDB. We should really a similar one in the observability space because in the o11y the databases also need to be as stable as possible as other people & businesses depend on them.

In debug and safe mode, Zig writes 0xaa bytes to undefined memory. This is to catch bugs early, and to help detect use of undefined memory in a debugger.

The fact that Zig writes 0xaa into undefined memory also helped a lot during development. If you step through some with gdb and spot just 0xaa being read then you know that that memory is undefined and you are missing some initializer somewhere.

Onto the next things to work on for this project: writing an encoder, fuzzing the decoder, writing some benchmarks, implementing lazy decoding.