Monday, October 2, 2023
HomeProgrammingNo surprises on any system: Q&A with Loris Cro of Zig

No surprises on any system: Q&A with Loris Cro of Zig


No surprises on any system: Q&A with Loris Cro of Zig

With so many languages to program in already, you may wonder why people still create new ones. But on last year’s Developer Survey, a new language called Zig showed up with a small following but the third-highest Admired score. To find out what this new language was about, I reached out to Loris Cro, VP of Community at the Zig Foundation and host of Zig Showtime, to talk about what makes Zig worth checking out.

This conversation has been edited for clarity and content.

——————

Ryan Donovan: How did you get involved in Zig?

Loris Cro: So before this job, I used to work at Redis Labs. I was a developer advocate, so part of my job was to show people how to do stuff with Redis, and I needed a lower level programming language to showcase how to use a specific feature of Redis. Redis has a plugin system, but to write a plugin, like a module, you have to create a dynamic library, which is something that only lower level programming languages can do.

So I tried with C, which was not a good experience, so I looked into newer languages. Of course I saw Rust, but I also saw Zig, and Zig really captured my attention. This is how I first learned about Zig.

I started using it and fast forward a few months, maybe a year, and the pandemic struck. In the meantime, I had gotten more involved with the community. I was running a show on Twitch called ZigShowtime because the idea was that as the pandemic started and the lockdown started, in person events transitioned into online events, except that they were basically becoming like extracurricular meetings. In my opinion, people weren’t doing the greatest job at translating, at changing medium.

The idea with Zig Showtime was to make an online meetup, but instead of it being a Zoom call, make it like a livestream show, with an intro, with music, with me acting as a host as if it was a real show. People liked it enough. Around the same time, Andrew Kelly, the creator of Zig, created theZigSoftwareFoundation, like a 501(c)(3) non profit and asked me to join him as VP of Community. So that’s how it started.

RD: What captured your attention about Zig?

LC: I think two main things, one technical and one, well, less so. The technical thing that really captured my attention when it comes to Zig was comptime. Zig does metaprogramming, but it doesn’t do metaprogramming with macros. Instead, it uses a different system called comptime.

Macros are very powerful, but they are also very brittle because they basically are programs that manipulate the syntax of your source code more or less. As I said, that’s very powerful, but it’s also very brittle because you’re messing around with text. Things lose some of their semantic meaning and just become tokens.

I like metaprogramming, and I think the macros are horrible. With comptime, what you do is use the language itself to manipulate part of the language in a more semantic way. You treat the language as a data structure, kind of like you do in Lisp, except it’s not a Lisp language. There are some significant differences.

The second one was watching Andrew, the creator, interact with people on GitHub. I noticed right away that he had a very effective approach to interacting with contributors and guiding newcomers, making sure that all the interactions end up being productive for both sides. I think this strength is part of the reason why Zig is where it is today, not just the technical reasons.

RD: There’s so many languages out there today, and so many mature ones. We always get a lot of Rust fans in thesurvey. Besides the metaprogramming, why do you think Zig came about?

LC: Why? Well, there’s an actual officially sanctioned story about the birth of Zig. Andrew wanted to create a digital audio workstation, a software tool to do real time manipulation of audio. That’s a very demanding domain because it needs to be real time. It needs to be fast. He tried a few different languages, tried Go, tried C, tried JavaScript. He always felt like they had shortcomings.

In the end, what he wanted was something more similar to C, but with the main obvious foot guns solved and improved. That’s when he started Zig.

RD: I noticed one of the guiding principles was simplicity, especially in terms of control flow and memory allocation, not having those surprises show up.

LC: Exactly. Because these are the kind of things that if you’re not careful about—if we go back to the digital audio workstation example, that’s what would make it impossible for you to make a good one.

RD: That seems to make it fast and almost deterministic. Like, you know what’s going to happen. There’s not going to be any dynamic surprises there.

LC: In my opinion, that’s maybe the greatest strength of C. In hindsight, C has a very long list of issues and bad things about it, but it also has a beauty. I think that the main component of C’s beauty is the fact that you can jump into some code and—assuming people didn’t go crazy with macros—you can easily tell what’s going on.

That’s because people can’t overload operators and can’t hide memory allocation. Actually with Zig, this is taken to an extreme that even C doesn’t do. For example, with C, you have functions that do allocate without necessarily you giving them an allocator, because they assume that `malloc` is available in Zig.

This is not the norm. In Zig, every data structure takes in an allocator explicitly. It’s even more obvious to understand which parts of your program allocate memory and which ones don’t

RD: So if you wanted to specifically allocate and limit memory, if you wanted to have the exact addresses of the memory, you could do that, right?

Loris: Yeah, but even more importantly, I would say that by always having a form of dependency inversion or injection when it comes to our allocators—having the caller specify the allocator—you also gain a lot of usability.

WebAssembly, for example, has strict limits on how memory is supposed to work. All the data structures can be used in WebAssembly, no exception, because everything that allocates can accept a WASM allocator when you are targeting WASM.

Similarly, for embedded data structures, you are in a very constrained environment, and you want to use a data structure that has some dynamic behavior in terms of memory allocation, but you don’t have a real heap. You can allocate a buffer on the stack and create a stack allocator, which basically uses a chunk of memory from the stack as its quote unquote heap. Then you pass that to other data structures, like hash maps and stuff.

Obviously, there are limits. You’re not able to use the data structure with the full flexibility compared to when you have a real 64 gigabyte heap. But it works. And it makes code much, much more reusable.

RD: You talked about the birth of Zig from the digital audio workstation, and there’s a lot of talk of the benefits of Zig on bare metal and making the standard library optional. Was there an intent in creating this and developing it to make it a basic bare metal language?

LC: Yeah, so the tagline they have for Zig—which I helped come up with—is “Zig is a general purpose programming language and tool chain for maintaining robust, optimal and reusable software.” We talked about reusability, right? That’s one main point. If you want robustness and optimality, your language has to be low level because, well, optimality is kind of self evident, right?

You want to have precise control over the machine. Otherwise, you’re not going to do the optimal thing. But also robustness comes from that. For example, if you write something in JavaScript and you run out of memory, your program will crash. As the person who writes JavaScript, you don’t have an option to not crash because your language will have built-in features that expect memory allocation to succeed. If that fails, all they can do is crash at the engine level. But with Zig, that doesn’t happen. You can write programs that never crash because they run out of memory.

One example of this is TigerBeetle, which is a distributed financial database, and they really care about robustness. For them, robustness is, of course, correctness, but also it’s a database. It’s really bad if it crashes, right? Even if it’s out of memory. What they do is they basically allocate a bunch of memory up front at startup. Then they never allocate any more after that.

This is really nice, because your process is not going to be killed because it goes out of memory. That’s a very nice property that gives extra robustness to your software.

There’s another keyword that we use in the description that people don’t really think much about, although it’s really important. We define Zig as a general purpose programming language.The idea behind Zig is that it’s not just for big computers, it’s also for embedded devices, hardware, all kinds of things that have a microcontroller in it. For everything that fits that description, the idea is that you should be able to program it with Zig.

RD: That’s what I thought when the standard library was optional. You want this programming language to be as small and as fast as possible. Do you have examples of Zig being used on devices?

LC: I can’t point you to a startup, but I can point you to a project called MicroZig, which is a project run by members of this community who are basically writing a hardware abstraction layer. The idea is that you have a Raspberry Pi, for example, and you just specify in the build script what type of board you have, and then you automatically through comptime get type definitions that match your board—with the same number of pins, and all the pins map to the physical device, et cetera. That should make developing for metadevices much easier.

Another thing that we really care about is cross compilation, so if you are on Linux, you can build an executable for my class or for Windows and vice versa. It’s nice, but it becomes critical when you’re dealing with embedded devices, because with embedded devices, you’re compiling on your computer and then uploading the binary blob to the device itself. So it’s always cross compilation in a sense.

We already had two Zig conferences in person, once with workshops, and we ran a Microsoft workshop twice. What happened with these workshops was that people would show up with Windows laptops, Mac OS, Linux machines, and all these combinations like Arm, x86, etc. Every single time, people could just plug the device in and it would always work on the first try. Every time you compile something with Zig, you can depend on system dependencies, but Zig makes it very easy to avoid doing that so that you can make your software really portable.

RD: That’s very cool. I think it’s interesting you say it’s a language and a tool chain. When we first were emailing, you were only talking about the compiler, and I know the compiler is a big part of the language. Can you talk about why the compiler is so important?

Loris: There are two aspects to it. One, more general, is what I just described, cross compilation. But more specifically, we don’t only support cross compilation of Zig code. We also support cross compilation of C and C code. So Zig is not only a Zig compiler, but it’s also a C compiler. Not everything is made from scratch.

What we do basically is we bundle Clang [Ed. note: Check out our podcast with Chris Lattner, the original creator of Clang]. Zig will depend on LLVM for optimizations, and since we already rely upon LLVM, it is easy for us to additionally mainly bundle Clang with Zig. If you run `zigcc zig`, the `cc` subcommand is a flag-compatible substitute for Clang. What occurs, although, is that you do not get instantly redirected to Clang.

Zig does some pre-processing of the flags and provides some further logic of its personal. For instance, for those who do cross compilation, it robotically units that up. As an instance that you simply’re cross compiling from Home windows to Linux. It robotically units up the import path for the Linux `libc`, despite the fact that you’re on Home windows. What compilers do, together with Clang usually, is that they assume they simply use the system one, which isn’t going to work for cross compilation.

It does caching. There’s additionally a couple of different issues that it does earlier than you get to Clang. Relating to C and C++, then it isn’t only a matter of the compiler itself and the `libc` dependency, there’s additionally a couple of extra instruments they should perceive the goal.

So, for instance, the linker does target-specific linking for Home windows, which is totally different from linking an executable for MacOS or Linux. There are a couple of different instruments like `ar` and some others that in apply, as a programmer, you do not actually care a lot about more often than not, however they should be there for cross completion to really work.

The software chain half is actual. It is a 40 megabyte tarball if you obtain it, and it accommodates a bunch of variations of `glibc`. That is one thing that normally compilers do not do for you. Not solely are you able to goal `glibc`, however you possibly can goal a particular model of `glibc`.

For instance, Ubermakes use ofZig to cross compile their Go packages as a result of they rely upon the system `libc`, they usually have some machines that require a particular model of `glibc`.

Mainly it is cross compilation, however not just for the language itself. For instance, Rust can cross compile Rust, however can’t cross compile C. In the event you rely upon a C library, then your expertise in the case of cross compilation is considerably totally different.

Identical with Go. There is a well-knownweblogsubmit that claims `cgo`—which is the command that you’d use to get to rely upon C libraries—shouldn’t be Go as a result of the cross compilation expertise is degraded.

However each Rust and Go can use Zig to supply C and C++ cross compilation so there are initiatives like CargoZigConstruct that mainly does this job and offers cross compilation of C dependencies.

RD: It seems like, by doing that, you may have an automated ecosystem to attract from, proper? You get all of the C libraries.

One other factor I needed to ask. You’re VP of neighborhood and clearly Andrew was huge on constructing neighborhood at the start. What is the Zig neighborhood appear like right this moment?

Loris: I believe that the 2 most fascinating issues that I can say concerning the Zig neighborhood and the challenge is that to start with, the challenge is BDFL run. In comparison with different languages, we’ve got a BDFL—Andrew—that has a last say on all design selections. He would not do all the things. He would not make each resolution utterly on his personal from the highest of his excessive tower. Different core contributors and collaborators do have a voice in what we do.

However for instance, folks generally suppose that when there is a GitHub concern that has a language characteristic proposal, they suppose that the variety of thumbs up influences the ultimate end result, nevertheless it does by no means. That is how the product is run at its core.

The communities round it are as a substitute very liberal, and there’s no official neighborhood and the neighborhood is decentralized, that means that there is no such thing as a official neighborhood place, other than GitHub. There’s, for instance, an enormous Discord server, nevertheless it’s not official. It isn’t run by Andrew or me. I am a moderator there, however I am not an proprietor, and the concept is that each neighborhood units up their very own code of conduct. They resolve how they wish to run their place.

That is, I believe, very totally different from how different languages are likely to do it, the place they have a tendency to have official locations, however we do not.

RD: Particularly ones with a benevolent dictator. They normally wish to centralize the neighborhood.

LC: Yeah. In our case, we do not.

RD: Lots of languages have arrange foundations to assist them. Are you able to discuss how the Zig Basis operates?

LC: One thing fascinating concerning the Zig Basis is it’s not a 501(c)(6). Lots of different nonprofits are mainly commerce organizations, whereas we’re a 501(c)(3), so we’re absolutely tax-exempt and pay the builders.

All the cash that we get, which is usually from donations, however sometimes additionally one thing else like assist contracts, 90-plus p.c of what we get goes to builders with the remainder getting used for infrastructure, CI, and administrative prices.

We have had two in-person conferences, so we’re nonetheless very small, however we had two conferences referred to as Software programYouCanLove. They are not nearly Zig, however they normally have two days devoted to Zig: one workshop day and one speak day devoted to Zig. Then there’s one speak day devoted to software program on the whole. That is additionally one other neighborhood constructing effort within the sense that it isn’t ZigConf. The thought is to place ahead concepts that are not particular to the language that we want for each committee member to all the time be mindful.

RD: You need the neighborhood to be greater than simply the language. Since you’re not simply utilizing the language on a regular basis.

LC: Precisely. And likewise to be open to different communities. What is the level of constructing software program? The purpose of constructing software program is that every one software program ultimately is for the utility of different people. And that is what defines it ultimately how good the software program is. That is the last word purpose, proper?

Final thing, Zig Showtime was on hiatus for a few years. It began in the course of the pandemic, then I received fatigued by on-line occasions terribly, like everyone, and that is after we shifted to in individual occasions and meetups, and so forth.

And now that we’re not fatigued anymore, I am restarting Showtime once more.

RD: I believe we’re all shifting previous the pandemic burnout stage.

LC: Yeah. It took some time and man, I used to be the host and I used to be getting bored with it. So no extra viewers.

RD: At all times a very good time to take a break.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments