Zig (programming language)


Zig is an imperative, general-purpose, statically typed, compiled system programming language designed by Andrew Kelley. It is intended to be a successor to the C programming language, with the goals of being even smaller and simpler to program in while also offering modern features, new optimizations and a variety of safety mechanisms while not as demanding of runtime safety as seen in other languages.[3] It is distinct from languages like Go, Rust and Carbon, which have similar goals but also target the C++ space.[4][5][6]

The improvements in language simplicity relate to flow control, function calls, library imports, variable declaration and Unicode support. Additionally, the language does not make use of macros or preprocessor instructions. Features adopted from modern languages include the addition of compile-time generic types, allowing functions to work on a variety of data, along with a small set of new compiler directives to allow access to the information about those types using reflection.

Another set of additions to Zig is intended to improve code safety. Like C, Zig does not include garbage collection and memory handling is manual. To help eliminate the potential errors that arise in such systems, it includes option types and simple syntax for using them. A testing framework is also designed into the language.

The primary goal of Zig is to "be pragmatic", in that it is intended to be a better solution to the sorts of tasks that are currently solved with C. A primary concern in that respect is readability; Zig attempts to use existing concepts and syntax wherever possible, avoiding the addition of different syntax for similar concepts. Additionally, it is designed for "robustness, optimality and maintainability", including a variety of features to improve safety, optimization and testing. The small and simple syntax is an important part of the maintenance, as it is a goal of the language to allow maintainers to debug the code without having to learn the intricacies of a language they might not be familiar with.[7] Even with these changes, Zig can compile into and against existing C code; C headers can be included in a Zig project and their functions called, and Zig code can be linked into C projects by including the compiler-built headers.[8]

In keeping with the overall design philosophy of making the code simple and easy to read, the Zig system as a whole also encompasses a number of stylistic changes compared to C and other C-like languages. For instance, the Rust language has operator overloading which means a statement like a = b + c might actually be a function call to a type's overloaded version of the plus operator. Additionally, that function might raise an exception which might pre-empt any following code. In Zig, if something calls a function, it looks like a function call, if it doesn't, it doesn't look like a function. If it raises an error, it is explicit in the syntax,[8] error handling is handled through error types and can be handled with catch or try.

The goals of Zig are in contrast to the many similar languages introduced in the 2020s time-frame, like Go, Rust, Carbon, Nim and many others. Generally, these languages are more complex with additional features like operator overloading, functions that masquerade as values (properties), and many other features intended to aid the construction of large programs. These sorts of features have more in common with C++'s approach, and these languages are more along the lines of that language.[8] Zig has a more conservative extension of the type system, supporting compile-time generics and accommodating a form of duck typing with the comptime directive.