Zig is a general-purpose language created in 2016 by
Andrew Kelly. Zig aims to fix the issues C language has while being pragmatic and simple at the same time. Zig really shines as being simple by not having any hidden control flows, not having hidden memory allocations, no pre-processors, and no macros. Basically, it's one language and one language only. Since pre-processors and macros can get really complex and become their own universe. Sure it's not the Zig Philosophy. Zig Compiler(Zig cc) also is a C compiler, and the interoperability from C to Zig is very good. Clearly, we can see influence from other languages like Go and Rust; however, Zig is pretty unique and different from Go and Rust. Zig is fast. Zig is perfect for system programming. I bet we will see a lot of solutions being built in Zig. As a languages aficionado, I like to learn at least one language per year and sometimes more; Zig was my 2021 pick, did not regret it. Even if you dont use all languages in production, you always learn new ideas and techniques; I need to say, I find it pretty fascinating.
Who is using Zig?
Uber is using Zig.TigerBeetle a finantial accounting Database. There is a Javascript runtime written in Zig called Bun. Bun's performance is quite appealing, thanks to Zig. It goes head-to-head (and even better) than Deno(Another JS runtime - but this one is written in Rust). Zig's default optimizer(backend) it's LLVM. Zig has a package manager called zigmod.
Performance is always great. The Zig compiler is also pretty fast and has a low memory footprint. The first thing that caught my attention in Zig was not the syntax or language flexibility but one feature called Comptime.
What is Comptime?
Which is a very fresh approach to meta-programming. Basically, you can control blocks of code that will be handled at compile time. There is a bunch of languages that perform optimizations for you under the hood and over time. Java JVM is pretty famous for JIT(Just in Time transformations and optimizations) that, in the long run, make the program much better. Several languages do inlining and improving the assembly generated. But it's pretty cool to see a language give you full control based on blocks for what you resolve in compile-time. Calling the function or block of code at compile-time is a pretty powerful technique and allows performance optimizations, such as manipulating types as values without runtime overhead.
It is a new language
The ecosystem is
small, and you won't find many answers on Stack overflow. But this will likely improve over time as more adoption kick in. IDE support is also pretty basic right now. Last year(2021), I discovered Zig and started doing some simple
POCs.
Cool Syntactic Sugar
Zig standard library has support for unit testing and json. Zig Lang has if, for, and switch expressions. Meaning that you can put ifs, for, and switches into return statements or even assign them to variables. Here is some code in Zig.
IF, For and Switch Expressions in Action in Zig Lang (Complete
code here)
Here we have some code for a console application in Zig. We have a main method, and there is a function called isEven, which receives a number, checks whether the number is even or not, and returns a boolean. As you can see, the is an IF statement in return.
In the main function on lines 6..10, we can see the For expression in action, where we are looping over an array called items of the type unsigned int 32. We are getting the value of the array by |value| and comparing if the value is even and equal to 20 if so, we break it - which means we will return to the variable. After the for we can add an else in the case of returns nothing.
Finally, we see the switch expression on lines 14..18.As you can see, we can do a powerful pattern matcher with the switcher expression, compare ranges of values, and even call other functions.
Zig is a language to be watched.
Cheers,
Diego Pacheco