Zabalo

Zig: C with intent

In online discussions, I often come across criticisms of Zig as a modern replacement to C. The issues mentioned typically contain references to other languages like Rust, C++, and even C itself as reasons to not bother switching to Zig. I won't go into the details of these discussions because I take a more simplistic view of Zig. I view Zig as C with intent, i.e., a language that improves on C by allowing the programmer to clearly express their intent using the language constructs. The goal of Zig is not to introduce complexity to remove the dangers of C but to clarify the ambiguity within C such that these issues do not manifest. A large part of this is due to its philosophy of:

  • No hidden control flow.
  • No hidden memory allocations.
  • No preprocessor, no macros.

Some examples are:

  • slices for working with constrained parts of memory
  • explicit allocators for specific use cases (e.g. arena, fixed buffer, leak detection, etc.)
  • defer for cleaning up resources not required at end of scope
  • error union types for handling errors or bubbling them up
  • tagged unions to specify which field is active
  • optional and null types to indicate when data is ready to be accessed
  • comptime to guarantee code is ran at compile time (e.g. for implementing generics)
  • easy C interop to leverage C's ecosystem through a safe wrapper

Many of these features are implemented in some way or another in the previously mentioned languages (some even enforce additional safety practices), but Zig keeps these features simple and accessible - and that makes all the difference. With simple syntax and low overhead, the developer is forced into writing robust code and the reviewer is able to accurately reason its intended behavior. This goes a long way in helping build safer systems.

As a bonus, Zig also has a "modern" standard library where many common data structures and networking utilities are available. It also includes a powerful build system that minimizes the effort required for packaging and distribution and simplifies cross compilation. Zig is a step up from C in all the right places.