AI distracted by Zig

Thought I would share an interesting interaction I had with Claude the other day. Here’s the full chat for reference: Claude

I was coming back to my zigx project (an X11 client written in Zig) and wanted to see if I could improve my support for various image formats. I asked Claude a few questions about the X11 protocol and then asked it to critique my existing code that iterates over all the supported formats. I hadn’t mentioned zig but the code I gave it was in zig and it ended it’s review with:

Are you implementing this in Zig? The syntax looks like Zig, which is an interesting choice for low-level X11 protocol handling.

It was funny having the AI do something that felt very human…we were on the topic of X11 but it got distracted because it noticed I might have been using Zig and wanted to know more. I also wasn’t sure if it was trying to say Zig was an “interesting choice” as a way of expressing skepticism of that choice so I had to ask it why it thought so and here was its reply:

Zig is an interesting choice for low-level X11 protocol handling for several reasons:

  1. Memory control: Zig gives you explicit control over memory allocation and layout without garbage collection, which is ideal for the precise buffer manipulation needed in X11 protocol handling.
  2. Error handling: Zig’s error handling model forces you to explicitly handle errors, which is crucial when dealing with network protocols where many things can go wrong.
  3. Compile-time features: Zig’s powerful compile-time evaluation allows you to generate protocol message definitions and parsers efficiently.
  4. Low-level capabilities with safety: Zig provides low-level control similar to C but with additional safety mechanisms like bounds checking and overflow detection, which helps prevent common bugs in protocol implementation.
  5. Cross-compilation: Zig’s built-in cross-compilation capabilities make it easier to build X11 clients for different platforms.
  6. No hidden control flow: Zig doesn’t have exceptions or hidden allocations, making it easier to reason about the exact sequence of operations in protocol handling.
  7. Small language footprint: The language is relatively simple compared to C++ or Rust, which can make the codebase more maintainable for protocol implementation.

Using Zig for X11 protocol handling puts you in a sweet spot between the raw performance of C and the safety features of more modern languages, without the complexity overhead of languages like Rust. This makes it particularly well-suited for systems programming tasks like implementing network protocols directly.

Wasn’t expecting it to start throwing shade on C++/Rust out of nowhere but there you go. I guess AI is a fan of Zig?

10 Likes

Oh for sure AI likes Zig. I found deepseek V3 to be a very capable zig assistant. Unfortunately it’s trained on an outdated version of zig, so if you use advanced stuff like anytype, anyopaque it always gets the concept right but the casting code wrong.

1 Like

What family and version of Anthropic Claude are you using? Sonnet-3.7 or 3.5 or lesser Haiku?

1 Like

According to Claude I’m using Claude 3.7 Sonnet. I’m not familiar with the various revisions and the differences but wouldn’t mind being enlightened.

I’m writing zig every day now with help from Claude via Cursor. I’ve found that if you maintain a small onboarding text file to put in context, and give Claude easy ways to tell when a request is “done”… I.e. The test passes, you can get into a very nice flow. It’s good to start a new chat every so often because of the context limits. Quite often the initial attempt at an edit (especially when Claude is modifying build.zig) will try to use an outdated API, but then it sees the compiler error and fixes it , explaining that it was using an outdated api…zig seems to be a nice language for “convert this to zig” style tasks. Enter… Enter… Enter… Enter… Done .

Anyone else have a zig workflow that leverages an AI assistant?

I’ve just checked Claude 3.7 about Zig version.

The response is “The latest stable version of Zig is 0.11.0, which was released in September 2023”.

So, It’s interested in general but Zig grows fast as you know and all code generated must be verified and in most changes will have broken changes

The thing about Claude=> it generates perfect Bash and Python code.
So, I’m doing my scripts on these languages just with prompts.

DevOps fixed! Finally :smiley:

Claude has no trouble with zig. It reads the standard library source if it needs to, analyzes lifetimes, fixes leaks and segfaults. It knows that things have changed for 0.14.0 as soon as it makes a build.zig change that uses the old API. This is what I’m seeing anyway.

Anthropic’s Claude LLM family consists of three LLM variants – Opus (the largest), Sonnet (sweet-spot medium) and Haiku (lightweight). I am working with Sonnet-3.5v2 daily and it has some knowledge of Zig-0.13. It helps to explicitly mention zig-0.13 in the prompt to pevent Sonnet falling back to v0.13.